why the cast is failing? - java

I am trying to do this
List<String> stringList = new ArrayList<String>();
stringList.add("one");
stringList.add("two");
stringList.add("three");
(String[]) stringList.toArray();
why does this gives me class cast exception ?
Caused by: java.lang.ClassCastException: [Ljava.lang.Object;

The reason why your code fails is because .toArray() returns an array of type Object[], which you cannot cast down into a String[]. To fix it, use the other toArray(T[] a) i.e.
stringList.toArray(new String[0]);

Better try this other method:
String[] stringArray = stringList.toArray(new String[stringList.size()]);
In that way the array returned is of the exact type that you need, no cast needed. The method you're invoking returns an Object[], not a String[] and the cast you're trying to perform is invalid.

Because toArray() method of List class returns Object[] not String[], and you cannot cast from Object[] to String[]. However, the right way to do it is:
String[] array = new String[stringList.size()];
stringList.toArray(array);
or:
String[] array = stringList.toArray(new String[stringList.size());

You cannot cast to a String array. You want:
String[] test = stringList.toArray(new String[stringList.size()]);

The ArrayList<T>.toArray() method returns an array of type Object, since that is a perfectly acceptable thing to do. In fact, because of type-erasure, it's the only thing it can do.
If you want to get an array of type String, then call the ArrayList<T>.toArray(T[]) method. In your case, you would call it like this:
stringList.toArray(new String[3]);
Calling it like that will stuff the ArrayList contents into the provided String array.

Related

Converting arraylist to array error in javafx [duplicate]

The following code (run in android) always gives me a ClassCastException in the 3rd line:
final String[] v1 = i18nCategory.translation.get(id);
final ArrayList<String> v2 = new ArrayList<String>(Arrays.asList(v1));
String[] v3 = (String[])v2.toArray();
It happens also when v2 is Object[0] and also when there are Strings in it.
Any Idea why?
This is because when you use
toArray()
it returns an Object[], which can't be cast to a String[] (even tho the contents are Strings) This is because the toArray method only gets a
List
and not
List<String>
as generics are a source code only thing, and not available at runtime and so it can't determine what type of array to create.
use
toArray(new String[v2.size()]);
which allocates the right kind of array (String[] and of the right size)
You are using the wrong toArray()
Remember that Java's generics are mostly syntactic sugar. An ArrayList doesn't actually know that all its elements are Strings.
To fix your problem, call toArray(T[]). In your case,
String[] v3 = v2.toArray(new String[v2.size()]);
Note that the genericized form toArray(T[]) returns T[], so the result does not need to be explicitly cast.
String[] v3 = v2.toArray(new String[0]);
also does the trick,
note that you don't even need to cast anymore once the right ArrayType is given to the method.
Using toArray from the JDK 11 Stream API, you can solve the more general problem this way:
Object[] v1 = new String[] {"a", "b", "c"}; // or array of another type
String[] v2 = Arrays.stream(v1)
.<String>map((Object v) -> v.toString()).toArray(String[]::new);
String[] str = new String[list.size()];
str = (String[]) list.toArray(str);
Use like this.

Different behaviour during converting a list of Strings to String array

I was working on Collection framework in Java , where i encountered a strange problem .
I made 2 lists of Strings 1 with the help of ArrayList while second was made using Arrays.asList(T ...).
After creation of these two list i tried to convert these lists into String arrays with the list.toArray() ,
as list.toArray() method call returns an object array , so i had to explicitly cast to String[] .
After casting some strange behaviour is happening as :
Case #1 : ( where list was created using ArrayList) , gives runtime exception as
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
Case 2 : (where list as created using Arrays.asList(T ... ) ) runs fine .
here is the code
String [] str = null ,str1 = null ;
List<String> list = new ArrayList<String>();
list.add("a");
List<String> list1 = Arrays.asList("a");
str = (String[]) list.toArray(); // Runtime Exception
str1 = (String[]) list1.toArray(); // Runs Fine
An ArrayList is backed by an Object[]. A copy of that array is returned with toArray().
Returns an array containing all of the elements in this list in proper
sequence (from first to last element).
It make no guarantees on the type of array returned. But we know this from the exception's message. If you want it to return a String[], use the overloaded method provided for this reason.
str = list.toArray(new String[0]);
The cast becomes unnecessary.
The List implementation returned by Arrays.asList maintains a reference to the array (implicit or explicit) passed as its variable arity argument.
Returns a fixed-size list backed by the specified array.
The invocation
List<String> list1 = Arrays.asList("a");
creates a String[] and passes that as the argument to asList. This isn't specified clearly by the API documention, but backed by seems to indicate that it will return that same array. (Looking at the implementation, it returns a clone.) Since it is a String[], there is no error when casting and assigning it to a variable of that type.
In both cases, the appropriate solution is to use the overloaded List#toArray(T[]).
For fun, run the following and check the type of array that is returned.
List<String> list1 = (List) Arrays.<Object> asList("a");
System.out.println(list1.toArray().getClass());
Don't make assumptions. Always rely on the API documentation. If it isn't clear, try to find a better solution.
The different calls to toArray are returning arrays with different component types. You can see this by running the following code:
List<String> list = new ArrayList<String>();
list.add("a");
List<String> list1 = Arrays.asList("a");
System.out.println(list.toArray().getClass());
System.out.println(list1.toArray().getClass());
On any version of Java 8 or earlier, the result is
class [Ljava.lang.Object;
class [Ljava.lang.String;
Basically this output means Object[] and String[] respectively.
However, this is a bug. See JDK-6260652. Although it's not stated very clearly, Collection.toArray() must return an Object[] and not an array of some subtype of Object. There are a couple reasons for this.
First is that Collection.toArray() was introduced in JDK 1.2, long before generics were added to the language. There was no possibility of any collection implementation returning anything other than Object[], so for compatibility, all collections' toArray() implementations must return Object[].
The second reason is that a rather offhand comment in the specification for toArray(T[]) says:
Note that toArray(new Object[0]) is identical in function to toArray().
which again requires toArray() to return Object[] and not an array of some other type.
This bug has been fixed in JDK 9. Running the code snippet above on a recent JDK 9 build gives the following output:
class [Ljava.lang.Object;
class [Ljava.lang.Object;
The fact that Arrays.asList("a") uses a String[] for internal storage is an implementation detail. The bug where toArray() returned something other than Object[] is this implementation detail leaking out. (In fact, the array is created by the varargs machinery, using the method's type parameter as the array component type. Arrays.asList() just wraps the array it's given.)
As others have said, if you want to control the component type of the returned array, use the other overload toArray(T[]):
String[] array = list.toArray(new String[0]);
String[] array1 = list1.toArray(new String[0]);
List<String> list = new ArrayList<String>();
list.add("a");
str = (String[]) list.toArray();
In this case your list invoke method toArray() of ArrayList class. Which looks like below, it returns Object []:
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
And elementData declare:
transient Object[] elementData;
And constructor method:
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
And DEFAULTCAPACITY_EMPTY_ELEMENTDATA:
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
There for, elementData is totaly Object [] and can't be casted to any type, String etc...
With Arrays.asList(T...), it returns java.util.Arrays$ArrayList class. And java.util.Arrays$ArrayList also has toArray() method. That subtle toArray() method makes some confuse :). Here is its implementation:
public Object[] toArray() {
return a.clone();
}
And finally a field declare:
private final E[] a;
java.util.Arrays$ArrayList.toArray() able to return Object [] and actually E []. Hope this will help you :)
The key here is that Arrays.asList(..) does not return a java.util.ArrayList, but instead it returns a java.util.Arrays$ArrayList. So the .toArray() methods vary slightly.
If you want the first case to return a String[], you can change the call to
str = list.toArray(new String[0]);

Convert java.util.Collections to Integer array

When I run my code I get the following error:
Caused by: java.lang.ClassCastException: java.util.Collections$UnmodifiableSet cannot be cast to [Ljava.lang.Integer;
The code that causes is:
Integer[] selects = (Integer[]) tbl_analytes.getValue();
But when I do this:
Object obj = tbl_analytes.getValue();
System.out.println(obj);
I get the following output:
[1,7,15]
I don't understand why I can't convert this to an Integer[]. Any tips?
The Collection interface has methods to convert Collections to arrays :
Object[] toArray();
or
<T> T[] toArray(T[] a);
You can't just cast an object one one type to an unrelated type and expect it to work.
The fact that printing the Set produces an output that looks like the output you get when printing an array doesn't mean anything.
final ArrayList<Integer> integerList = new ArrayList<>();
final Integer[] integerArray = new Integer[integerList.size()];
integerList.toArray(integerArray);
It seems that 'tbl_analytes' is a Set, not an array. You can not cast it, you have to convert it:
Integer[] array = set.toArray(new Integer[0]);
You have to use the toArray(T a[])-method defined in the Collection interface, passing an empty Integer[] to the method. This solution assumes, that your List is of type List<Integer>. Otherwise, this would fail.
Set<Integer> values = (Set<Integer>) tbl_analytes.getValue();
The class cast exception gives the actual type. So use that or better a generalisation (List instead of ArrayList etcetera).
In the java language arrays are primitive. Collections are not.
Since a set is unordered it has no way to understand how to build an ordered array from a hashed set of memory locations.
When you output a set you are calling the toString method on that set.
So:
When I call
Integer[] selects = (Integer[]) tbl_analytes.getValue();
I get the following error:
Caused by: java.lang.ClassCastException: java.util.Collections$UnmodifiableSet cannot be cast to [Ljava.lang.Integer;
But when I do this:
Object obj = tbl_analytes.getValue();
System.out.println(obj);
I get the following output
[1,7,15]
The reason is Integer[] is completely uncastable from Set<Integer> but
the output from toString is derived from:
The java.util.Collections$UnmodifiableSet extends java.util.AbstractCollection
https://docs.oracle.com/javase/10/docs/api/java/util/AbstractCollection.html#toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).
That is the resason you are getting the exception. The solution to it is answered here in this stack overflow question:
How to convert Set<String> to String[]?

Collection.toArray() method, detail about it

Why would not it work?
List<String> lista = new ArrayList<>();
lista.add("Lol");
lista.add("ball");
String [] array = (String[])lista.toArray();
It throws a RunTimeException (ClassCastException), I am aware that there is another method for the purpose of returning the object contained in the List, however what is happening behind the scenes? I mean I am casting an array of Objects which actually is an array of Strings to an Array of Strings. So it should compile, but it does not.
Thanks in advance.
That version of toArray() returns Object[]. You can't cast an Object array into a String array even if all the objects in it are Strings.
You can use the lista.toArray(new String[lista.size()]); version to get the actual type correctly.
List.toArray()
returns an Object[], because of type erasure. At runtime your list does not know if it has String objects. From there you can see where that error is coming from.
You cannot type cast an Object[] into a String[]
Array of objects is not array of Strings and can't be cast to one.
Check this.
use toArray(T[] a) instead.
Ie.
List<String> lista = new ArrayList<String>();
lista.add("Lol");
lista.add("ball");
String [] array = lista.toArray(new string[1]);
This insures that toArray returns an array of type String[]
As others have noted, toArray() returns an array of type Object[], and the cast from Object[] to String[] is illegal.
List lista = new ArrayList<>(); ---> List lista = new ArrayList();
There are two toArray() versions.You can use another one!

java: (String[])List.toArray() gives ClassCastException

The following code (run in android) always gives me a ClassCastException in the 3rd line:
final String[] v1 = i18nCategory.translation.get(id);
final ArrayList<String> v2 = new ArrayList<String>(Arrays.asList(v1));
String[] v3 = (String[])v2.toArray();
It happens also when v2 is Object[0] and also when there are Strings in it.
Any Idea why?
This is because when you use
toArray()
it returns an Object[], which can't be cast to a String[] (even tho the contents are Strings) This is because the toArray method only gets a
List
and not
List<String>
as generics are a source code only thing, and not available at runtime and so it can't determine what type of array to create.
use
toArray(new String[v2.size()]);
which allocates the right kind of array (String[] and of the right size)
You are using the wrong toArray()
Remember that Java's generics are mostly syntactic sugar. An ArrayList doesn't actually know that all its elements are Strings.
To fix your problem, call toArray(T[]). In your case,
String[] v3 = v2.toArray(new String[v2.size()]);
Note that the genericized form toArray(T[]) returns T[], so the result does not need to be explicitly cast.
String[] v3 = v2.toArray(new String[0]);
also does the trick,
note that you don't even need to cast anymore once the right ArrayType is given to the method.
Using toArray from the JDK 11 Stream API, you can solve the more general problem this way:
Object[] v1 = new String[] {"a", "b", "c"}; // or array of another type
String[] v2 = Arrays.stream(v1)
.<String>map((Object v) -> v.toString()).toArray(String[]::new);
String[] str = new String[list.size()];
str = (String[]) list.toArray(str);
Use like this.

Categories