2017-05-01 66 views
-1

追加我有在循環中使用下面的StringBuilder作爲輸出不一致的問題。請幫忙!!!不一致的StringBuilder在一個循環

String oldStr = "job1.job2.job3"; 
String[] newStr = oldStr.split(Pattern.quote(".")); 
StringBuilder result = new StringBuilder(); 
for (String temp : newStr) {  
    String key = "This is "+keyStr+" ,"; 
    result.append(key); 
    System.out.println(key.toString()); 
} 

我希望結果是

This is job1. 
This is job1. This is job2. 
This is job1. This is job2. This is job3. 

我沒有看到預期的結果,在一個點,但隨後我看到這些現在:

This is job1. 
This is job2. 
This is job3. 

如果我繼續運行代碼,我看到了多個元素:

This is job1. 
This is job2. 
This is job3. 
This is job1. 
This is job2. 
This is job3. 

是否有問題,我的代碼或StringBuilder的是不是因爲它應該正常運作? TIA。

回答

0

改變這一行

System.out.println(key.toString()); 

System.out.println(result.toString()); 
+0

非常感謝。不知道我怎麼可能會錯過。現在,這對我的作品。 –