想把一个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就会不停的添加,请问我该怎么设计这个中间变量,如果不这么做,还有别的方法吗?哪位高手帮帮忙

解决方案 »

  1.   

    hash_temp.clear(); //把hash_temp清空
    换成这个hash_temp=new HashSet();
      

  2.   

    但是为什么会是这样呢?hash_temp.clear()与hash_temp=new HashSet()有什么区别吗?
      

  3.   

    还有一个问题:
               HashMap hash = hash_array[0]; 
              hash.add("c");
    执行完后hash_array[0]为什么也会加上"c"呢?怎样才不改变hash_array[0]的值呢
    请大家帮帮忙