简体   繁体   English

如何根据订单从ArrayList获取不常见的元素

[英]How to get uncommon elements from ArrayList based on order

I am comparing two ArrayLists based on the Order 我正在根据Order比较两个ArrayLists

The size of total_list will be always bigger than the sub_list total_list的大小将始终大于sub_list

import java.util.ArrayList;

import org.json.JSONException;

public class MrTest {

    public static void main(String args[]) throws JSONException
    {



        ArrayList<String> total_list = new ArrayList<String>();
        ArrayList<String> sub_list = new ArrayList<String>();

        ArrayList<String> un_common_list = new ArrayList<String>();

        total_list.add("Beverages");
        total_list.add("Hot");
        total_list.add("Sai1");
        total_list.add("Sai2");
        total_list.add("Sai3");


        sub_list.add("Sai1");
        sub_list.add("Sai2");
        sub_list.add("Sai3");




        for(int i=0;i<total_list.size();i++)
        {

            String total_name = total_list.get(i);
            String sub_name = sub_list.get(i);

            if(total_name.equals(sub_name))
            {

            }
            else
            {
                un_common_list.add(total_name);
            }


        }


        System.out.println(un_common_list);
    }
}

I am getting 我正进入(状态

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at MrTest.main(MrTest.java:35)

where as i am expecting the output as 我期待输出的地方

[Beverages, Hot]

Could anybody pleasse help me , as how to get Output as 任何人都可以帮助我,就像如何获得输出一样

[Beverages, Hot] in this case ??

I think you just want: 我想你只想要:

total_list.removeAll(sub_list);

If you want to preserve total_list : 如果要保留total_list

List<String> uncommon = new ArrayList<>(total_list);
uncommon.removeAll(sub_list);

You are using the same index for both lists, causing the sublist to go out of bounds. 您对两个列表使用相同的索引,导致子列表超出范围。 Use a loop within a loop, and use a boolean to check if it matches any of the values, if it didnt match, then it should add the string to the uncommon list. 在循环中使用循环,并使用布尔值检查它是否与任何值匹配,如果它不匹配,则应将该字符串添加到不常见列表中。

boolean inlist=false;
 for(int i=0;i<total_list.size();i++)
    {

        String total_name = total_list.get(i);

        for(int k=0;k<sub_list.size();k++)
        {
            String sub_name = sub_list.get(k);


            if(total_name.equals(sub_name))
            {
              inlist=true;
            }

        }
       if (!inlist){un_common_list.add(total_name);}
       inlist=false;
  }
    for(int i=0;i<total_list.size();i++) 
    // Total_list size is 5
    {

        String total_name = total_list.get(i);
        String sub_name = sub_list.get(i);
        // sub_list size is 3, 
        // but you are calling sublist.get(3), sublist.get(4) and sublist.get(5)
    }

i would go with 1 loop, first make a copy of total_list using System.arraycopy to un_common_list, then iterate the sub_list and remove elements present in sub_list from the un_common_list, order should be preserved 我将使用1循环,首先使用System.arraycopy将total_list的副本复制到un_common_list,然后迭代sub_list并从un_common_list中删除sub_list中存在的元素,顺序应该保留

however the removeAll() should preserve order too, just use the array copy thing 但是removeAll()也应该保留顺序,只需使用数组副本

如果要保持插入顺序,那么可以尝试这种情况

for(int i=0;i<total_list.size() && i<sub_list.size();i++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM