VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1047|回复: 5

循环问题;

[复制链接]

1

主题

1

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2020-3-8 01:30:01 | 显示全部楼层 |阅读模式
如下:
ArrayList al=new ArrayList();
                        al.Add("lei");
                        al.Add("wanglei");
                        al.Add("wanghao");
                        al.Add("wanglei");
                        al.Add("wanghao");

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

使用道具 举报

1

主题

12

帖子

11.00

积分

新手上路

Rank: 1

积分
11.00
发表于 2020-5-28 12:30:02 | 显示全部楼层
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()
回复

使用道具 举报

0

主题

26

帖子

20.00

积分

新手上路

Rank: 1

积分
20.00
发表于 2020-5-28 13:00:01 | 显示全部楼层
好像只能没条进行判断了~~~
回复

使用道具 举报

0

主题

4

帖子

3.00

积分

新手上路

Rank: 1

积分
3.00
发表于 2020-5-28 14:15:01 | 显示全部楼层
ArrayList al2=new ArrayList();
for(int i=0;i<al.Count;i++)
{
  if(!al2.Contrains(al[i])
       al2.Add(al[i]);
}               

al = al2;       
回复

使用道具 举报

0

主题

322

帖子

115.00

积分

新手上路

Rank: 1

积分
115.00
发表于 2020-5-28 14:45:01 | 显示全部楼层
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>");
                        }
回复

使用道具 举报

0

主题

119

帖子

67.00

积分

新手上路

Rank: 1

积分
67.00
发表于 2020-5-28 20:30:01 | 显示全部楼层
楼上方法不错!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表