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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Bharat Gaikwad
  • 530
  • 2
  • 5
  • 16

4 Answers4

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.

powerMicha
  • 2,753
  • 1
  • 24
  • 31
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.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 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 – Bharat Gaikwad Jul 14 '11 at 11:17
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"

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Mobile Developer
  • 5,730
  • 1
  • 39
  • 45
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()]);
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • thanks man, ur solutions always enlight my understanding of language beyond my questions(jimmy's Question, I know), thanks..... – Bharat Gaikwad Jul 14 '11 at 11:25
  • arrList to new ArrayList() doesn't work, used the last option..... same error java.lang.ClassCastException – Bharat Gaikwad Jul 14 '11 at 12:15
  • 1
    I just wrote this and it works for me: `List arrList = new ArrayList(); Integer[] ints = arrList.toArray(new Integer[arrList.size()]);`. – Buhake Sindi Jul 14 '11 at 12:25
  • 1st option just removes note of unchecked operations @compile time, 2nd option does the work, Why?? – Bharat Gaikwad Jul 14 '11 at 12:27
  • Because of the method `List.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. – Buhake Sindi Jul 14 '11 at 12:33
  • yes it works for me too... but have to do both, 1st and 2nd, ur 1st and my old constructor doesn't work it gives the error @runtime, but removes note of unchecked operations... – Bharat Gaikwad Jul 14 '11 at 12:34
  • forcing arrList to be Integer type and using intArr=(Integer[])arrList.toArray(); gives runtime error... but now things are sort out,thanks – Bharat Gaikwad Jul 14 '11 at 12:54