简体   繁体   中英

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 Collection s 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[]?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM