0

I would like fill an external array with the elements of my ArrayQueue via E[] toArray(E[] a) , but somehow it throws ArrayStoreException at the first System.arraycopy method. I would like to know how to solve this issue, and even more important for me to know why this exception is thrown.

Here's my code:

public E[] toArray(E[] a)
{
    if(a.length != size) 
        a=(E[])Array.newInstance(a.getClass(), size); 
    if(head<tail)
        System.arraycopy(elementData, head, a, 0, tail-head); // ArrayStoreException
    else
    {
        System.arraycopy(elementData, head, a, 0, capacity-head);
        System.arraycopy(elementData, 0, a, capacity-head, tail);
    }
    return a;           
}

This is the external method:

String[] words = q.toArray(new String[2]);

Thanks for your time.

4
  • 1
    Could you show a minimal reproducible example demonstrating the issue? (Is this in a generic type? I assume so...) The simpler you make it for us to reproduce, the easier it is to help you. Commented Mar 12, 2016 at 21:43
  • (Actually, I suspect I know the problem - just checking now...) Commented Mar 12, 2016 at 21:43
  • Sorry, but i do not have a clue what extra information should i provide to solve this. My ArrayQueue is generic, it has a field E[] elementData which contains the elements. The underlying type of elementData is Object, and it is casted in the consturctor to (E[]) Commented Mar 12, 2016 at 21:54
  • The extra information would be a short but complete example - something we could copy, paste, compile and run to see the problem. Commented Mar 12, 2016 at 21:56

2 Answers 2

2

I suspect the exception doesn't actually happen on the line you've indicated, but later, in System.arraycopy.

The problem is that your call to Array.newInstance passes in the array type, when you only want to pass in the element type. In other words, you're saying "Give me a new array with an element type String[]" where you really want to say "Give me a new array with an element type String".

To do this, just use getClass().getComponentType(). Demo which is a simpler but complete example of the problem, if you remove getComponentType():

import java.lang.reflect.*;

public class Test {

    public static void main(String[] args) {
        String[] original = new String[] { "x" };
        String[] x =  Test.<String>toArray(original);
    }

    public static <E> E[] toArray(E[] a) {
        E[] copy = (E[]) Array.newInstance(a.getClass().getComponentType(), a.length + 1);
        System.arraycopy(a, 0, copy, 0, a.length);
        return copy;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

a.getClass() will not return E, it will return an array of E (since a is an array). this is why you're having trouble with the assignment

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.