1

在遍历“哈希图列表”之后,我想将值写入字符串作为“值 1”和“值 2”。我不知道怎么做?有人可以帮我吗!

for (Object listItem : (List)value) {
System.out.println(key + ":" + listItem);

我的数据中有 2 个值。上面的代码给了我

骗子:姓名

缺点:ID

现在,我希望 'Name' 的值为 1,而 'ID' 的值为 2,这样我就可以替换它们并将它们写入以下字符串

"xxxxxxxxxx"+key+"xxxx"+value 1+"xxxxxxxxx"+value 2+"xxxxxxxxxx";
4

2 回答 2

1

尝试:

String value1="";
String value2="";
int counter=0;

for (Object listItem : (List)value) {
    System.out.println(key + ":" + listItem);

    if(counter==0) {//first pass assign value to value1
       value1=listItem; 
       counter++;//increment for next pass
    }else if(counter==1) {//second pass assign value to value2
        value2=listItem;
        counter++;//so we dont keep re-assigning listItem for further iterations
    }

}

System.out.println(value1);//should display 'Name'
System.out.println(value2);//should display 'ID'
于 2012-08-05T16:53:59.017 回答
0

使用StringBuilder类(假设key在循环之外定义)

StringBuilder sb = new StringBuilder("xxxxxx" + key + "xxxx");
for (Object listItem : (List)value) {
    System.out.println(key + ":" + listItem);
    sb.append(listItem+"xxxxxxxxx");
}
于 2012-08-05T16:54:14.320 回答