Android 如何比较两个不同数组的对象? for(int i=0;i

Android 如何比较两个不同数组的对象? for(int i=0;i,android,Android,我必须比较来自2个不同数组列表的mcategory_id和scategory_id的值。如何比较这两个值?我应该用什么样的逻辑 如果您关心顺序,可以使用列表的“equals”方法 List mcategoryIdList=new ArrayList(); List SCATEGORYDLIST=新建ArrayList(); for(int i=0;i

我必须比较来自2个不同数组列表的mcategory_id和scategory_id的值。如何比较这两个值?我应该用什么样的逻辑

如果您关心顺序,可以使用列表的“equals”方法

List mcategoryIdList=new ArrayList();
List SCATEGORYDLIST=新建ArrayList();
for(int i=0;i
如果不关心顺序,请使用集合:

List mcategoryIdList = new ArrayList();
List scategoryIdList = new ArrayList();
for (int i = 0; i < jsonArray.length(); i++) {
    //retrieve json objects
    JSONObject Jasonobject = jsonArray.getJSONObject(i);
    JSONObject jsonObject1 = jsonArray1.getJSONObject(i);

    //add to lists
    mcategoryIdList.add(Jasonobject.getString("category_id"));
    scategoryIdList.add(jsonObject1.getString("category_id"))
}

//now you can compare both
if(mcategoryIdList.equals(scategoryIdList)){
    //they're equal
}else{
    //they're not equal
}
Set mcategoryIdSet=new HashSet();
Set scategorydiset=new HashSet();
for(int i=0;i
阅读有关杰克森或JSONASSERT的信息,您可以提供更多详细信息。我在代码中没有看到ArrayList。似乎只有一个,items,您可以在这里收集所有疫苗接种数据。我使用的是来自json的两个不同数组列表。我必须比较McCategory_id和scategory_id…请检查我的CodeInvesturementData是否是模型类的对象
List mcategoryIdList = new ArrayList();
List scategoryIdList = new ArrayList();
for (int i = 0; i < jsonArray.length(); i++) {
    //retrieve json objects
    JSONObject Jasonobject = jsonArray.getJSONObject(i);
    JSONObject jsonObject1 = jsonArray1.getJSONObject(i);

    //add to lists
    mcategoryIdList.add(Jasonobject.getString("category_id"));
    scategoryIdList.add(jsonObject1.getString("category_id"))
}

//now you can compare both
if(mcategoryIdList.equals(scategoryIdList)){
    //they're equal
}else{
    //they're not equal
}
Set mcategoryIdSet = new HashSet();
Set scategoryIdSet = new HashSet();
for (int i = 0; i < jsonArray.length(); i++) {
    //retrieve json objects
    JSONObject Jasonobject = jsonArray.getJSONObject(i);
    JSONObject jsonObject1 = jsonArray1.getJSONObject(i);

    //add to sets
    mcategoryIdSet.add(Jasonobject.getString("category_id"));
    scategoryIdSet.add(jsonObject1.getString("category_id"))
}

//now you can compare both
if(mcategoryIdSet.equals(scategoryIdSet)){
    //they're equal
}else{
    //they're not equal
}