2011-06-18 38 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了解更多信息。

相关问题