日期:2014-05-20  浏览次数:20464 次

循环问题;
如下:
ArrayList   al=new   ArrayList();
al.Add( "lei ");
al.Add( "wanglei ");
al.Add( "wanghao ");
al.Add( "wanglei ");
al.Add( "wanghao ");

如果我想除去里面的重复的   应该怎么做啊   ,注意   ,里面al里面的值的条数是不确定的啊!~

------解决方案--------------------
1.The removeDuplicate Method:
/** List order not maintained **/
public static void removeDuplicate(ArrayList arlList)
{
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
}


2.The removeDuplicateWithOrder Method:
/** List order maintained **/
public static void removeDuplicateWithOrder(ArrayList arlList)
{
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = arlList.iterator(); iter.hasNext(); )
{
Object element = iter.next()
------解决方案--------------------
ArrayList al2=new ArrayList();
for(int i=0;i <al.Count;i++)
{
if(!al2.Contrains(al[i])
al2.Add(al[i]);
}

al = al2;
------解决方案--------------------
ArrayList al=new ArrayList(); al.Add( "lei "); al.Add( "wanglei "); al.Add( "wanghao "); al.Add( "wanglei "); al.Add( "wanghao "); al.Sort(); for(int i=1;i <al.Count;i++) { if(al[i].ToString() == al[i-1].ToString()) { al.RemoveAt(i); i--; } } for(int i=0;i <al.Count;i++) { Response.Write(al[i].ToString()+ " <BR> "); }