0

I am trying to copy a new array at the end of the one i have, but it keeps saying ArrayStoreException in the System.arraycopy and i dont know why, it should have space and everything.

this is the code:

objectzombie=zombieParser(result); /*give a 4 object array*/
GameObjectList arrayt= new GameObjectList(objectzombie.size()+this.objectlist.size());

System.arraycopy(objectzombie, 0, arrayt, 0, objectzombie.size());
System.arraycopy(this.objectlist, 0, arrayt, arrayt.size(), 
this.objectlist.size());
this.objectlist=arrayt;}

thank you for your help;

EDIT-------------

It looks like, i can't use the arraycopy if i don´t have a primitive array, how can i then combine my two lists? i don't know how could i do it.

3
  • 1
    please show the relevant code and the relevant stacktrace, you are showing way too little information Commented Dec 5, 2018 at 12:38
  • According to documentation: Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects Commented Dec 5, 2018 at 12:46
  • Possible duplicate of ArrayStoreException in java arrays Commented Dec 5, 2018 at 12:49

1 Answer 1

1

According to Java SE API documentation, Arguments of System.arraycopy are:

  • src - the source array.
  • srcPos - starting position in the source array.
  • dest - the destination array.
  • destPos - starting position in the destination data.
  • length - the number of array elements to be copied.

In the method description, there is also:

Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:

  • The src argument refers to an object that is not an array.

  • The dest argument refers to an object that is not an array.

  • The src argument and dest argument refer to arrays whose component types are different primitive types.

  • The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type.

  • The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type.

the src and dest are array, whereas you tried to copy to target object of type GameObjectList, which is a mismatch => ArrayStoreException

Sign up to request clarification or add additional context in comments.

3 Comments

I see, so i guess i can't use this. I will try to find another way then... but i cant think about one.
I modified the answer more clearly just now. You can read again to get to know how to use the core Java API :)
Thank you, but i nees to copy the arraylist one, so i guess i will have to use another thing🤔

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.