Ive got an ArrayList with String, int and double in it, a total of 20 elements.
Im trying to move the Strings to a String-array, the ints to an int-array and the doubles to a double-array. But I cant figure out how, this is what I have for fnding Integers:
private ArrayList <Object> randomList = new ArrayList <Object>();
private int [] intArray = new int[10];
public void example(){
createArray();
for(int i = 0; i<randomList.size();i++){
if(randomList.get(i)instanceof Integer){
intArray[i]= (Integer) randomList.get(i);
}
}
}
I cant understand why this dosnt work, it tries to add objects that arent Integers into my array causing an outOfBounds exception, since its trying to add more then 10 elements.
You have index mismatch, that’s all. Try something like:
You should also check whether the current index for a particular array is less than the upper boundary, which is the initial size of that array: