0

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.

2
  • Mixing different types in the same list is a code smell. Why do you want that in the first place? Commented Nov 26, 2012 at 21:06
  • cus I was bored and annoyed that I couldnt figure it out. Commented Dec 13, 2012 at 13:26

5 Answers 5

2

You have index mismatch, that's all. Try something like:

int index = 0;

for(int i = 0; i<randomList.size();i++)
{
        if(randomList.get(i)instanceof Integer)
        {
            intArray[index++]= (Integer) randomList.get(i);
        }
}

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:

if(randomList.get(i) instanceof Integer && index < intArray.length)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to maintain a separate index into each of your output arrays.

Comments

0

Well first of all you should never have allowed all these data types to congregate in on ArrayList of objects. But if you already have them:

for(Object o : randomList)
{
    if(o instanceof Integer)
    {
        //add to inteeger list
    }
    else if(o intenaceof String)
    {
       //add to string list
    }
    //...etc
}

Comments

0
private int [] intArray = new int[10];

Your array has a size of 10, but your list does not have a size limit.

private int [] intArray = new int[randomList.size()];

might work.

Comments

0
  1. Try initializing your array as :

    private int [] intArray = new int[randomList.size()];
    
  2. Try another index to assign values to array:

    int count=0;
    for(int i = 0; i<randomList.size();i++){
        if(randomList.get(i)instanceof Integer){
           intArray[count++] = (Integer) randomList.get(i);
        }
    }        
    
  3. In the end, you may want to truncate the array fragment which was unused using System.arraycopy as below:

    private int [] finalArray= new int[count;
    System.arraycopy(intArray, 0, finalArray, 0, count);
    

1 Comment

@MrCharli3L Just be careful of the situations when you have more that 10 integers in your list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.