JAVA: Preventing Duplicate Entries to an ArrayList

前端 未结 6 1727

I am trying to prevent duplicate entries from being added to an ArrayList as the list is being populated whilst reading through each line of a file. Each line of the file is

6条回答
  •  梦毁少年i
    2021-01-12 01:07

    ArrayList ar=new ArrayList();
    String a[]={"cat","bat","cat","knife"};
    for(int i=0;i

    Create an array list, and check whether it contains the string to be inserted. If it does not contain the string, then you can add it into the array list. This way you can avoid the duplicate entries in the array list.

    The elements in the array list for the above code would be

    cat bat knife

提交回复
热议问题