我正在制作一种方法,需要将一个数组复制到另一个数组中。

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);
  }
}


但是当我执行它时出现:

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)

最佳答案

System.arraycopy在两个数组之间复制-您将其应用于SuperList的两个实例,这是一个集合(可能是实现List)。

关于java - 使用数组副本的数组存储异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19063600/

10-10 22:29