How do I remove repeated elements from ArrayList?

后端 未结 30 2248
难免孤独
难免孤独 2020-11-21 06:24

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

30条回答
  •  野性不改
    2020-11-21 06:38

    If you are using model type List< T>/ArrayList< T> . Hope,it's help you.

    Here is my code without using any other data structure like set or hashmap

    for (int i = 0; i < Models.size(); i++){
    for (int j = i + 1; j < Models.size(); j++) {       
     if (Models.get(i).getName().equals(Models.get(j).getName())) {    
     Models.remove(j);
       j--;
      }
     }
    }
    

提交回复
热议问题