ArrayList去除重复项

 //去重复方法一
        private ArrayList RemoveDuplicateNum(ArrayList list)
        {
            list.Sort();
            for (int i = 0; i < list.Count - 1; i++)
            {
                if (list[i].Equals(list[i + 1]))
                {
                    list.RemoveAt(i);//去重复项  
                    i--;
                }
            }
            return list;

        }
        //去重复方法二
        private ArrayList RemoveSame(ArrayList list)
        {
            //上面写的那句是多余的,这个是最终的  
            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    if (list[i].Equals(list[j]))
                    {
                        list.RemoveAt(j);
                        j--;
                    }
                }
            }
            return list;
        }

 

//////////////////////////////////

public static void main(String[] args){
  ArrayList list = new ArrayList();
  list.add("1"); list.add("2"); list.add("2");
  Iterator it1 = list.iterator();
  Hashtable ht = new Hashtable();
  while(it1.hasNext()){
   Object obj = it1.next();
   ht.put(obj, obj);
  }
 
  Iterator it2 = ht.keySet().iterator();
  list = new ArrayList();
  while(it2.hasNext()){
   list.add(it2.next());
  }

 

posted @ 2013-04-10 10:07  金虹巴巴  阅读(606)  评论(0编辑  收藏  举报