标题:ArrayList的使用,删除其中为 5 的数
只看楼主
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
结帖率:78.95%
已结贴  问题点数:26 回复次数:3 
ArrayList的使用,删除其中为 5 的数
求助,
不懂它是要怎么查找,网上说是indexOf()
但是不清楚他的具体用法
谢谢
搜索更多相关主题的帖子: 网上 
2016-04-30 21:58
franksking
Rank: 2
等 级:论坛游民
帖 子:16
专家分:43
注 册:2016-4-25
得分:26 
ArrayList list = new ArrayList();
for(int i=0;i<list.size();i++)
{
  if(list.get(i)==5)
{
 list.remove(i);
}
}
2016-05-03 16:26
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
得分:0 
import java.util.*;

public class ArrayList_Operation_3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Integer> a = new ArrayList<>();

        for(int i=0 ; i<20 ; i++)
        {
            Random rand = new Random();
            int n = rand.nextInt();
            n = rand.nextInt(10);
            a.add(n);
        }
        Iterator arr = a.iterator();
        while(arr.hasNext())
        {
            int p = (int)arr.next();
            System.out.print(p+" ");
        }
        for(int i=0;i<arr.size();i++)
        {
            if(arr.get(i)==5)
            {
                  arr.remove(i);
            }
        }
        for(int i=0 ; i<3 ; i++)
        {
            Random rand = new Random();
            int n = rand.nextInt();
            n = rand.nextInt(10);
            a.add(4+i,n);
        }
    }
   
}
2016-05-04 23:13
the_second
Rank: 2
等 级:论坛游民
帖 子:115
专家分:80
注 册:2015-9-13
得分:0 
为什么我的函数这样是错的
2016-05-04 23:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-464281-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.248870 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved