2015-02-17 143 views
9

有没有什么办法可以用快捷方式在try-catch块外面拉变量?例如:提升Intellij IDEA?

来自:

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

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

回答

11

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

  1. 把你的光标放在t1,然后“显示意图行动“。从那里选择“拆分为声明和分配”。您的代码现在看起来像这样:

    try { 
        AbstractList<String> t1; 
        t1 = new ArrayList<String>(); 
    } catch (Exception e) { 
        e.printStackTrace(); 
    } 
    
  2. 将光标置于声明行。
  3. 做“向上移动声明”操作。现在,你的代码看起来就像这样:

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

谢谢。顺便提一下,上移动作是ctrl-shift-up。 – vikingsteve 2015-02-18 08:09:33

+0

@vikingsteve是啊我感觉不好我没有给键盘快捷键,但我在Mac上,并习惯了PC,所以我改变了很多我的设置。我的快捷键与除我以外的任何人无关。 – 2015-02-18 18:08:11