2014-07-16 38 views
3

在調試器中,您可以按alt-f8並評估表達式。如何在Intellij調試器中使用代碼片段模式?

還有一個代碼片段模式。 IntelliJ中的文檔只說:

代碼片段模式評估簡短代碼部分介紹他們 在語句評估文本字段。支持的構造是 聲明,賦值,循環和if/else。

我在網上找不到任何關於如何使用它的例子,並且找不到我自己。

您可以舉例說明如何使用受支持的結構嗎?

回答

5

鑑於

public class CodeFragment { 
    public static void main(String[] args) { 
     List<Foo> list = new ArrayList<Foo>(); 
     list.add(new Foo("555")); 
     list.add(new Foo("777")); 
     list.add(new Foo("999")); 
     list.add(new Foo("bill")); 

     System.out.println(); 
    } 

    public static class Foo { 
     String s; 

     public Foo(String s) { 
      this.s = s; 
     } 
    } 
} 

如果我們設置println的一個破發點,我們可以把下列代碼片段

Foo resultFoo = null; 
Iterator<Foo> it = list.iterator(); 
while (it.hasNext()) { 
    Foo foo = it.next(); 
    if (foo.s.equals("777")) { 
     resultFoo = foo; 
    } 
} 
resultFoo = resultFoo; 

這表明聲明,分配,循環,以及如果。

請注意,舊版本的intellij不支持foreach循環!

還要注意最後的分配。據我所知,顯示的結果是最後一條語句的結果。如果沒有最後一條語句,這段代碼會顯示'false' - 即最後一次調用it.next的結果。

相關問題