Laura
Laura

Reputation: 31

Array Store Exception using array copy

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)

Upvotes: 0

Views: 1329

Answers (1)

Mureinik
Mureinik

Reputation: 311823

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

Upvotes: 1

Related Questions