0

Run time exception-- java.lang.ClassCastingException...

Integer intArr[] = new Integer[arrList.size()];
ArrayList <Integer> arrList =new ArrayList();
intArr=(Integer[])arrList.toArray(); // returns Object class which is downcaste to Integer;

I understand down-casting is not safe but why is this happening? I also tried to converting ArrayList to String to Integer to int, but I get the same error.

4
  • can ArrayList contents be converted directly into int Commented Jul 14, 2011 at 10:43
  • No, as int is a primitive type and ArrayList<int> cannot be done. In other words, Generics cannot be applied on primitive types. Commented Jul 14, 2011 at 10:46
  • @The Elite Gentleman thanks, just was wanted 1 line solution for converting ArrayList into primitive types... Commented Jul 14, 2011 at 11:02
  • You will have to write a wrapper that does convert Integer[] to int[]. Commented Jul 14, 2011 at 11:14

4 Answers 4

5

Try to do this

intArr = arrList.toArray(new Integer[arrList.size()]);

What you get is a typed Integer Array and not a Object array.

Sign up to request clarification or add additional context in comments.

Comments

1

First of all, this doesn't bind the ArrayList to type Integer.

ArrayList <Integer> arrList =new ArrayList();

Instead, this is what happens, arrList is assigned to an ArrayList of raw type, but that isn't a problem.

The problem lies in,

intArr=(Integer[])arrList.toArray();

since arrList is a raw-type (due to the assignment, it gets assigned as new ArrayList<Object>() by the compiler), you're effectively getting an Object[] instead.

Try assigning arrList to new ArrayList<Integer>() and do this:

intArr = arrList.toArray(new Integer[arrList.size()]);

7 Comments

thanks man, ur solutions always enlight my understanding of language beyond my questions(jimmy's Question, I know), thanks.....
arrList to new ArrayList<Integer>() doesn't work, used the last option..... same error java.lang.ClassCastException
I just wrote this and it works for me: List<Integer> arrList = new ArrayList<Integer>(); Integer[] ints = arrList.toArray(new Integer[arrList.size()]);.
1st option just removes note of unchecked operations @compile time, 2nd option does the work, Why??
Because of the method List<T>.toArray(T[] array);. Because I've said that arrList is type Integer, the toArray method expects and Integer array (at compile-time). The implementation returns all internal values of the list to an array. In other words, the Generic type T is bounded to an Object Integer. Study Generics.
|
1

The problem here is that you are trying to convert an array of objects to an array of integers. Array is an object in itself and Integer[] is not a sub-class of ArrayList, nor vice versa. What you have to do in your case is cast individual items, something like this:

Integer intArr[] = new Integer[arrList.size()];
for(int i=0; i<intArr.length; i++)
{
    intArr[i] = (Integer)arrList.get(i);
}

Naturally, you may get ClassCastException if individual elements in the array list are not of type Integer.

1 Comment

thanks, individual items a general solution of problems, any class conversion, but for here i found a constructor as specified by powerMicha,MaciejK or The Elite Gentleman... thanks
1

toArray(T[] a) takes a paramter: "a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runt"

Comments

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.