拉变量Intellij IDEA?

时间:2015-02-17 21:13:52

标签: intellij-idea refactoring

有没有办法用快捷方式将变量拉到try-catch块之外?例如:

从:

try{
    AbstractList<Type> t1 = new ArrayList<Type>();
} catch (Exception e) {
    ...
}

AbstractList<Type> t1;
try{
    t1 = new ArrayList<Type>();
} catch (Exception e) {
    ...
}

1 个答案:

答案 0 :(得分:14)

我知道如何通过一些快捷方式来做到这一点:

  1. 将光标放在t1上然后&#34;显示意图操作&#34;。从那里,选择&#34;拆分为声明和分配&#34;。您的代码现在看起来像这样:

    try {
        AbstractList<String> t1;
        t1 = new ArrayList<String>();
    } catch (Exception e) {
        e.printStackTrace();
    }
    
  2. 将光标放在声明的行上。
  3. 执行&#34;移动声明&#34;行动。现在您的代码将如下所示:

    AbstractList<String> t1;
    try {
        t1 = new ArrayList<String>();
    } catch (Exception e) {
        e.printStackTrace();
    }