读书人

ArrayListlt;String[]gt; 的有关问题

发布时间: 2012-03-21 13:33:15 作者: rapoo

ArrayList<String[]> 的问题
这是我写的一段代码:
import java.util.*;

public class test{
public static void main(String[] args){
String type = "aaa ";
String name = "sss ";

String[] conds = new String[2];
conds[0] = type;
conds[1] = name;

ArrayList <String[]> list = new ArrayList <String[]> ();
list.add(conds);

type = "pp ";
name = "mm ";

conds[0] = type;
conds[1] = name;

list.add(conds);

for(int m = 0; m < list.size(); m++){

String[] tmp = new String[2];
tmp = list.get(m);
System.out.println( "type: " + tmp[0]);
System.out.println( "name: " + tmp[1]);
}//end for
}//end main
}

运行结果是
pp
mm
pp
mm
而不是想要的:
aaa
sss
pp
mm
我估计是因为list.add(conds)时加入ArrayList的仅仅是conds这一变量,而并没有将此时conds指向的字符串数组加入ArrayList。
那么我需要怎么做才能将实际的String[]加入到ArrayList中呢?
希望大家帮忙,谢谢

[解决办法]
重新new一个
[解决办法]
将conds[0] = type;
conds[1] = name;
list.add(conds);
改成下面代码就可以了
String[] conds1 = new String[2];
conds1[0] = type;
conds1[1] = name;

list.add(conds1);

读书人网 >J2SE开发

热点推荐