黑马程序员技术交流社区

标题: 关于集合的问题 [打印本页]

作者: 张豪杰    时间: 2013-2-17 22:46
标题: 关于集合的问题
想把一个HashMap中的值放入hashMap数组中,如:把一个hashMap{"1,2","2,3"}转换成一个hashMap[],数组中的形式是这样的
{ {“1”“ 2”},{“1”“ 2”} },我写的代码是这样的:
     public static void main(String[] args)
{
HashSet test = new HashSet();
String string = new String();
test.add("1,2");
test.add("2,3");
         int n =0;
String[] stemp = new String[20];
HashSet hash_temp = new HashSet();
//HashSet hash_temp2 = new HashSet();
HashSet[] hash_array = new HashSet[test.size()];  //要转换成的hash数组
Iterator iterator = test.iterator();
while(iterator.hasNext())
{
stemp = iterator.next().toString().split(",");//解析test中的每一项,使由“1,2”形 式变成“1”“ 2”形式
int j=0;
for (int i = 0; i < stemp.length; i++)
{
hash_temp.add(stemp[i]);
}
j++;
hash_array[n] = hash_temp;

stemp = null;
hash_temp.clear();   //把hash_temp清空
n++;
}

//printf hash_array
for (int i = 0; i < hash_array.length; i++)
{
Iterator it = hash_array[i].iterator();
System.out.println("hash_array"+i+":");
while(it.hasNext())
{
System.out.println(it.next().toString());
}
}
可是若把中间变量hash_temp清空,得到的hash_array就会是空的,若不清空hash_temp就会不停的添加,请问我该怎么设计这个中间变量,如果不这么做,还有别的方法吗?


作者: 李桐    时间: 2013-2-17 23:38
额 有必要么...




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2