Java ArrayList removed in the same elements and keep the same elements in the last

Realization of ideas: the list objects cycle twice, to get data comparison and data inside the outer layer, the outer layer of the same deletion (outer element certainly forward than memory), if the same thing, remove the outer layer of the data, so that the final output outside layer data list, the result can be guaranteed to be unique, and retains the latter elements.

 

List list = new ArrayList();
  Object s = new Object();
  for(int i = 0;i<5;i++){
   list.add(s);
  }
  for(int i = 0;i<list.size();i++){//循环list
         for(int j = i+1;j<list.size();j++){
                 if(list.get(i).equals(list.get(j))){
                        list.remove(i);//删除一样的元素
                        i--;
                        break;
                 }
         }
  }
  for(int i = 0;i<list.size();i++){
   System.out.println(list.get(i));
  }

 

Guess you like

Origin www.cnblogs.com/itzyz/p/11057851.html