A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张豪杰 中级黑马   /  2013-2-17 22:46  /  1083 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

想把一个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 个回复

正序浏览
额 有必要么...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马