我能够获取json响应,在arraylist中添加了响应
当我尝试比较2个arraylist时,arraylist2值没有变化。
我做了什么:
json解析:
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
id = obj.getString("alert_id");
map.put("alert_id", id);
Log.d("map", map.toString());
arraylist1.add(map);
Log.e("asdasd", arraylist1.toString());
}
- 比较:
for (int i = 0; i < arraylist.size(); i++) {
for (int j = 0; j < arraylist1.size(); j++) {
if (!(arraylist.get(i).get("id")).equals((arraylist1.get(j).get("id")))) {
//Checking whether the last value in the list..
if (j == (arraylist1.size() - 1)) {
unCommonList.add(arraylist.get(i).get("alert_id"));
} else {
}
}
}
}
- 输出:
92 79
91 79
86 79
85 79
84 79
81 79
80 79
- 每次arraylist2保持不变,arraylist1正在改变,但arraylist2值不是
- 这是我的arraylist2:
[{abcd=100, id=79, createdate=2016-03-14 03:59:43, defge=2012310, 123123xyzw=alpha}, {abcd=11230, id=79, createdate=2016-03-14 03:59:43, defge=201231230, xyzw=alpasdha}, {abcd=10452430, id=79, createdate=2016-03-14 03:59:43, defge=12346, xyzw=alasdpha}, {abcd=1za00, id=79, createdate=2016-03-14 03:59:43, defge=212312300, xyzw=alpdsaha}, {abcd=112312300, id=79, createdate=2016-03-14 03:59:43, defge=21231200, xyzw=alpsqwha}, {abcd=123123, id=79, createdate=2016-03-14 03:59:43, defge=201231230, xyzw=alpqwreha}, {abcd=112312300, id=79, createdate=2016-03-14 03:59:43, defge=212312300, xyzw=alpyjha}, {abcd=101231230, id=79, createdate=2016-03-14 03:59:43, defge=200, xyzw=alpha}]
答案 0 :(得分:1)
for (int i = 0; i < arraylist.size(); i++) {
for (int j = 0; j < arraylist1.size(); j++) {
if (!(arraylist.get(i).get("id")).equals((arraylist1.get(j).get("id")))) {
//Checking whether the last value in the list..
if (j == (arraylist1.size() - 1)) {
unCommonList.add(arraylist.get(i).get("alert_id"));
} else {
}
}else{
break;
}
}
}
答案 1 :(得分:1)
好的,为了获得不匹配的元素,你可以试试这段代码
ArrayList<Integer> results = new ArrayList<>();
for (Person person2 : arrayList2) {
boolean found = false;
for (Person person1 : arrayList1) {
if (person2.id == person1.id) {
found = true;
}
}
if (!found) {
results.add(person2.id);
}
}