快速輔助

快速輔助會執行區域碼轉換。它們是在 Java 編輯器中選項或單一游標上呼叫的,它使用與快速修正程式 (Ctrl+1) 相同的捷徑,但在出現錯誤時,快速輔助通常會隱藏起來。 如果即使同一行出現錯誤也要顯示它們,請再次按 Ctrl+1

您可以將快速輔助選項指派給直接捷徑。 依預設,它們是:

請在 開啟按鍵喜好設定頁面 一般 > 按鍵喜好設定頁面中(位於「來源」種類),指派其他快速鍵或是變更預設快速鍵。

您可以在 開啟「Java 編輯器」喜好設定頁面 Java > 編輯器喜好設定頁面中,開啟快速輔助燈泡。

名稱 程式碼範例 呼叫位置
反 if 陳述式 if (x) a(); else b(); > if (!x) b(); else a(); 在有 else 區塊的 if 陳述式上
轉換成 if-!-return if (x == 1) a(); > if (x != 1) return;
a();
在 if 陳述式上
反 Boolean 表示式 a && !b > !a || b 在 Boolean 表示式上
反轉區域變數 boolean a = false;
if (a) {}
> boolean notA = true;
if (!notA) {}
在 boolean 變數上
反向等於 a.equals(b) > b.equals(a) 在 equals 的呼叫上
反條件表示式 x ? b : c > !x ? c : b 在條件式表示式上
拉出否定句 b && c > !(!b || !c) 在 Boolean 表示式上
推回否定句 !(b && c) > !b || !c 在否定的 Boolean 表示式上
移除額外的括弧 if ((a == b) && (c != d)  {} > if (a == b && c != d)  {} 在選取的表示式上
將表示式放在括弧中 return a > 10 ? 1 : 2; > return (a > 10) ? 1 : 2; 在選取的表示式上
將表示式放在括弧中 if (a == b && c != d)  {} > if ((a == b) && (c != d))  {} 在選取的表示式上
結合巢狀 if 陳述式 if (a) { if (b) {} } > if (a && b) {} 在巢狀 if 陳述式上
交換巢狀 if 陳述式 if (a) { if (b) {} } > if (b) { if (a) {} } 在巢狀 if 陳述式上
用 and'ed 表示式分割 if 陳述式 if (a && b) {} > if (a) { if (b) {} } 在 if 中的 and'ed 表示式上
結合選取的 'if' 陳述式和 || if (a) x();  if (b) x(); > if (a || b) x(); 在選取的 'if' 陳述式上
在 if-else-if 中,結合 'if' 序列 if (a) x();  if (b) y(); > if (a) x();  else if (b) y(); 在選取的 'if' 陳述式上
用 or'd 表示式分割 if 陳述式 if (a || b) x(); > if (a) x();  if (b) x(); 在 if 中的 or'd 表示式上
If-else 指派給條件表示式 if (a) x= 1; else x= 2; > x= a ? 1 : 2; 在 if 陳述式上
If-else 傳回條件表示式 if (a) return 1;
else return 2;
> return a ? 1 : 2; 在 if 陳述式上
條件表示式指派給 If-else x= a ? 1 : 2; > if (a) x= 1; else x= 2; 在條件式表示式上
條件表示式傳回 If-else return  a ? 1 : 2; > if (a) return 1; else return 2; 在條件式表示式上
切換至 If-else switch (kind) {
case 1: return -1;
case 2: return -2;
}
> if (kind == 1) {
  return -1;
} else if (kind == 2) {
  return -2;
}
在 switch 陳述式上
將 if-else 轉換成 switch if (kind == 1) {
  return -1;
} else if (kind == 2) {
  return -2;
}
> switch (kind) {
case 1: return -1;
case 2: return -2;
}
在 if 陳述式上
在列舉上新增遺漏的 case 陳述式 switch (e){
}
> switch (e){
case E1: break;
case E2: break;
}
在 switch 陳述式上
交換運算元 a + b > b + a 在中置運算上
強制轉型和指派 if (obj instanceof Vector) {
}
> if (obj instanceof Vector) {
 Vector vec= (Vector)obj;

}
在 'if' 或 'while' 陳述式中的 instanceof 表示式上
使用個別的 catch 區塊 try {
} catch (FileNotFoundException | InterruptedIOException e) {
}
> try {
} catch (FileNotFoundException e) {
} catch (InterruptedIOException e) {
}
在 multi-catch 區塊上(1.7 或更新版本)
將異常狀況移動至個別的 catch 區塊 try {
} catch (FileNotFoundException | InterruptedIOException | IllegalArgumentException e) {
}
> try {
} catch (FileNotFoundException e) {
} catch (InterruptedIOException | IllegalArgumentException e) {
}
在 multi-catch 子句中的異常狀況上(1.7 或更新版本)
合併 catch 區塊 try {
} catch (FileNotFoundException e) {
} catch (InterruptedIOException e) {
}
> try {
} catch (FileNotFoundException | InterruptedIOException e) {
}
在 catch 區塊上(1.7 或更新版本)
新增 finally 區塊 try {
} catch (Exception e) {
}
> try {
} catch (Exception e) {
} finally {}
在 try/catch 陳述式上
新增 else 區塊 if (a) b(); > if (a) b(); else { } 在 if 陳述式上
用區塊取代陳述式 if (a) b(); > if (a) { b(); } 在 if 陳述式上
未展開區塊 { a() } > a() 在區塊、if/while/for 陳述式上
結合為單一字串 String phrase= "one" + " two " + "three"; > String phrase= "one two three"; 在字串連結表示式上
辨識字串 "abcdefgh" > "abc" + "de" + "fgh" 選取部分字串文字
將字串連結轉換成 StringBuilder (J2SE 5.0) 或 StringBuffer "Hello " + name > StringBuilder builder= new StringBuilder();
builder.append("Hello ");
builder.append(name);
選取字串文字
將字串連結轉換成 MessageFormat "Hello " + name > MessageFormat.format("Hello {0}", name); 選取字串文字
分割變數 int i= 0; > int i; i= 0; 在有初值設定的變數上
結合變數 int i; i= 0; > int i= 0 在沒有初值設定的變數上
指定給變數 foo() > X x= foo(); 在表示式陳述式上
擷取至區域 foo(getColor()); > Color color= getColor();
foo(color);
在表示式上
指派參數給欄位 public A(int color) {} > Color fColor;
public A(int color) {
    fColor= color;
}
在參數上
建立陣列起始設定元到陣列 int[] i=  { 1, 2, 3 } > int[] i= new int[] { 1, 2, 3 } 在陣列起始設定元上
建立 'for' 迴圈 void foo(Map<String, Integer> map) {
   map.keySet();
}
> void foo(Map<String, Integer> map) {
  for (String string : map.keySet()) {
  }
}
在陣列上,CollectionList
轉換成「針對迴圈加強」(J2SE 5.0) for (Iterator i= c.iterator();i.hasNext();) {
}
> for (x : c) {
}
在 for 迴圈上
轉換成檢索的 'for' 迴圈 (J2SE 5.0) for (x : c) {
}
> for (int i = 0; i < c.size(); i++) {
    x = c[i];
}
在針對迴圈加強上
轉換成反覆運算子型 'for' 迴圈 (J2SE 5.0) for (x : c) {
}
> for (Iterator i= c.iterator();i.hasNext();) {
}
在針對迴圈加強上
超類別中的 Create 方法


在方法宣告上
在檔案中重新命名


在 ID 上
在工作區中重新命名


在 ID 上
擷取至區域變數 a= b*8; > int x= b*8;
a= x;
在表示式上
擷取至常數 a= 8; > final static int CONST= 8;
a= CONST;
在表示式上
擷取方法 int x= p * 5; > int x= getFoo(p); 在表示式和陳述式上
列入區域變數 int a= 8, b= a; > int b= 8; 在區域變數上
將區域變數轉換成欄位 void foo() { int a= 8; } > int a= 8; void foo() {} 在區域變數上
將匿名轉換成巢狀類別 new Runnable() { }; > class RunnableImplementation implements Runnable { } 在匿名類別上
轉換成 lambda 表示式 Runnable r= new Runnable() {
  public void run() {}
};
> Runnable r= () -> {}; 在實作功能介面的匿名類別上(1.8 或更新版本)
轉換成匿名類別建立 Runnable r= () -> {}; > Runnable r= new Runnable() {
  public void run() {}
};
在 lambda 表示式上(1.8 或更新版本)
將主體表示式變更為區塊 Runnable r= () -> System.out.println(); > Runnable r= () -> {
  System.out.println();
};
在以主體作為表示式的 lambda 表示式上(1.8 或更新版本)
將主體區塊變更為表示式 Runnable r= () -> {
  System.out.println();
};
> Runnable r= () -> System.out.println(); 在以主體作為區塊的 lambda 表示式上(1.8 或更新版本)
取代為 getter 和 setter(封裝欄位) p.x; > p.getX(); 在欄位上
插入推斷類型引數 List<String> list = new ArrayList<>(); > List<String> list = new ArrayList<String>(); 在一般實例建立表示式上(1.7 或更新版本)

內容檔編輯器中提供下列快速輔助:

相關概念

Java 編輯器
快速修正程式和快速輔助

相關參照

快速修正程式
JDT 動作