注册 登录
编程论坛 JAVA论坛

ArrayList的使用,删除其中为 5 的数

the_second 发布于 2016-04-30 21:58, 2135 次点击
求助,
不懂它是要怎么查找,网上说是indexOf()
但是不清楚他的具体用法
谢谢
3 回复
#2
franksking2016-05-03 16:26
ArrayList list = new ArrayList();
for(int i=0;i<list.size();i++)
{
  if(list.get(i)==5)
{
 list.remove(i);
}
}
#3
the_second2016-05-04 23:13
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);
        }
    }
   
}
#4
the_second2016-05-04 23:13
为什么我的函数这样是错的
1