2011-06-18 73 views
0

所以我在尋找一種方法來暫時存儲值,以便他們可以在需要時刪除將int添加到字符串名稱的結尾?

我創造了18串(我可能會對此完全錯誤的方式,以便糾正我,如果我錯了!):INFO1 ,info2,info3等...

我想設置每一個到一個特定的值取決於用戶所在的洞,這是我如何描繪它。

hole = 1; 
info + hole = current; <--- current is a string with a value already. 
hole++; 

(所以INFO1 =電流值1)

info + hole = current; <--- current is a new string with a new value similar to the first. 
hole++; 

(所以INFO2 =電流值2)

如果您需要更多的代碼,請讓我知道。我決定我會跳過它,並不打擾社區的問題,所以我刪除了代碼,然後決定不,我真的想要這個功能。如果需要的話,我會很快重寫它。

回答

3

這是一種錯誤的做法

info + 1 = 2; 

不一樣

info1 = 2; 

你需要把事情的數組和操作,然後

因此,對於你18串定義數組as

String[] info = new String[18]; 

再後來做

info[hole-1] = current; 

這裏是基本陣列不錯的教程在Java FYI http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

+0

它應該是'info [hole-1] = current',因爲字符串數組是零索引的。 –

+0

好趕上,編輯它,謝謝! –

+0

真棒謝謝你! – Rob

1

做一個String陣列:

String[] info = new String[18]; 
// .... 
hole = 1; 
info[hole] = current; 
hole++; 
0

即語法錯誤。在處理大量變量時應該使用數組或列表。在這種情況下,製作String陣列。這是你的代碼應該如何看起來像:

String info[] = new String[18]; 
String current = "something"; 
int hole = 1; 
info[hole-1] = current; // string gets copied, no "same memory address" involved 
hole++; 

更短的代碼片段:

String info[] = new String[18], current = "something"; 
int hole = 1; 
info[hole++ - 1] = current; // hole's value is used, THEN it is incremented 

去走遍this official documentation tutorial瞭解更多信息。

相關問題