有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无法应用java系统copyarray对象>int[]?

我试着:

private int[] data = new int[1];
int[] newArr = new int[1];
System.arraycopy(data, 0, newArr, data.length);

但我得到了:

error: method arraycopy in class System cannot be applied to given types;
                System.arraycopy(data, 0, newArr, data.length);
                      ^
  required: Object,int,Object,int,int
  found: int[],int,int[],int
  reason: actual and formal argument lists differ in length
1 error

Compilation Failed

我确信拷贝任何我做错的东西都可以吗


共 (2) 个答案

  1. # 1 楼答案

    你只是错过了方法的最后一个参数,here'sjavadoc,下面是它在参数中所说的:

    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.

    下面应该是可行的:

    int[] data = new int[1];
    int[] newArr = new int[1];
    System.arraycopy(data, 0, newArr, 0, data.length);
    System.out.println(Arrays.toString(newArr));
    
  2. # 2 楼答案

    如前所述,你错过了一场辩论。但由于要复制整个阵列,还可以使用更简单的方法:

    int[] newArr = data.clone();