문제

I am trying to do copy from a LinkedBlockingQueue to a dataStuff[] data array using .toArray() but I am getting an Exception

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LdataStuff;
    at Main.main(Main.java:30)

with the following code

public static BlockingQueue<dataStuff> recurseFragments = new LinkedBlockingQueue<dataStuff>();

    public static void main(String args[]) throws IOException
    {
        dataStuff[] data = (dataStuff[]) recurseFragments.toArray();
    }

I understand that its putting the recurseFragments into an object[] before it spits it into the array but why is it that casting does not work and how can I solve this?

도움이 되었습니까?

해결책

Try using the other toArray method

dataStuff[] data = recurseFragments.toArray(new datastuff[0]);

다른 팁

Try this:

dataStuff[] data = recurseFragments.toArray(new dataStuff[0])

Using this signature of toArray you can get the correct type of return.

Because toArray() returns an Object[]

Object[] toArray();

So , you should use toArray(T[] a) instead.

<T> T[] toArray(T[] a);

Use it this way:

recurseFragments.toArray(new datastuff[0]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top