0

I'm making a method in which I require to copy an array to another.

public void rotate (int movements){
  SuperList<T> temp = new SuperList<> ();
  if( movements != size ){
     for( int i = 0; i < size - movements; i++){
        temp.add( i, (T) (get( movements + i ))); 
        //System.out.println(i + movements);
     }
     for( int j = 0; j < movements; j++)
        temp.add( temp.size(), ( T ) (get( j )));
     System.arraycopy(temp, 0, this, 0, size);
  }
}

but when I execute it appears:

Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at unal.datastructures.SuperList.rotate(SuperList.java:42)
at unal.datastructures.SuperList.main(SuperList.java:65)
3
  • Can you show the main()..? Commented Sep 28, 2013 at 6:36
  • Why is your if condition stating if( movements != size ).? . Where is size.? Commented Sep 28, 2013 at 6:39
  • Do you know there is Collections.rotate()? Commented Sep 28, 2013 at 22:35

1 Answer 1

1

System.arraycopy copies between two arrays - you are applying it to two instances of SuperList, which is a collection (implements List, presumably).

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

2 Comments

SuperList extends from ArrayLinearList, in ArrayLinearList is implemented System.arraycopy and it works properly.
Assuming you mean this, you are using it wrong. ArrayLinearList uses System.arraycopy to copy its element data member, which is indeed an array (of T). It does not apply it to object references such as this or temp like you have in the code snippet you enclosed.

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.