Java 为什么会抛出IndexOutOfBoundsException?
为什么在下面的代码段中抛出IndexOutOfBoundsException? 我似乎不明白它为什么会被扔出去Java 为什么会抛出IndexOutOfBoundsException?,java,exception,arraylist,indexoutofboundsexception,Java,Exception,Arraylist,Indexoutofboundsexception,为什么在下面的代码段中抛出IndexOutOfBoundsException? 我似乎不明白它为什么会被扔出去 import java.util.*; public class PrimeNumbers { //Printing all prime numbers less than 600 using 'Sieve Method' final static int SIZE = 600; static ArrayList<
import java.util.*;
public class PrimeNumbers {
//Printing all prime numbers less than 600 using 'Sieve Method'
final static int SIZE = 600;
static ArrayList<Integer> numbers = new ArrayList<Integer>(SIZE);
public static void populateList(ArrayList<Integer> arraylist){
for(int i=0; i<SIZE; i++){
arraylist.add(i, i);
}
}
public static void filterMultiples(ArrayList<Integer> arraylist){
for(int i=0; i<SIZE; i++){
if(arraylist.get(i)%2==0 || arraylist.get(i)%3==0 || arraylist.get(i)%5==0){
arraylist.remove(i);
}
}
}
public static void main(String[] args){
populateList(numbers);
filterMultiples(numbers);
System.out.println(numbers);
}
}
for(int i=0;i此块中存在错误:
for(int i=0; i<SIZE; i++){
if(arraylist.get(i)%2==0 || arraylist.get(i)%3==0 || arraylist.get(i)%5==0){
arraylist.remove(i);
}
}
}
for(int i=0;i
for(int i=0; i<SIZE; i++){
if(arraylist.get(i)%2==0 || arraylist.get(i)%3==0 || arraylist.get(i)%5==0){
arraylist.remove(i);
}
}
}
for(int i=0; i<SIZE; i++){
if(arraylist.get(i)%2==0 || arraylist.get(i)%3==0 || arraylist.get(i)%5==0){
arraylist.remove(i);
}
}
}
for (Iterator<Integer> iterator = numbers.iterator(); iterator.hasNext();) {
int number = iterator.next();
if (number % 2 == 0 || number % 3 == 0
|| number % 5 == 0) {
iterator.remove();
}
}