去除重复元素

  1: package cn.cqu.huang;
  2: 
  3: import java.util.ArrayList;
  4: import java.util.List;
  5: 
  6: public class MyArrayList {
  7: 	
  8: 	//去除重复元素
  9: 	public static void main(String[] args){
 10: 		List<String> lst = new ArrayList<String>();
 11: 		lst.add("aaa");
 12: 		lst.add("bbb");
 13: 		lst.add("ccc");
 14: 		lst.add("aaa");
 15: 		lst.add("ddd");
 16: 		lst.add("ddd");
 17: 		lst.add("ddd");
 18: 		
 19: 		for(int i=0;i<lst.size();i++){
 20: 			String tem = lst.get(i);
 21: 			if(i!=lst.lastIndexOf(tem)){
 22: 				lst.remove(lst.lastIndexOf(tem));
 23: 				i--;
 24: 			}
 25: 		}
 26: 		
 27: 		System.out.println(lst);
 28: 	}
 29: 	
 30: 	
 31: }
 32: 

执行结果:

image

原文地址:https://www.cnblogs.com/wyhuang/p/3917975.html