Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置一个数组';s值到另一个数组';Java中的s值?_Java_Arrays - Fatal编程技术网

如何设置一个数组';s值到另一个数组';Java中的s值?

如何设置一个数组';s值到另一个数组';Java中的s值?,java,arrays,Java,Arrays,假设您有两个阵列: int[] a = {2, 3, 4}; int[] b = {4, 5, 6}; 如何将数组a设置为数组b并使它们保持不同的对象?就像我想做的那样: a = b; 但这不起作用,因为它只生成“a”引用数组b。那么,在使两个数组保持独立对象的同时,将两个数组设置为相等的唯一方法是循环遍历一个数组的每个元素并将其设置为另一个吗 那么ArrayList呢?当一个ArrayList中有对象时,如何将其设置为另一个ArrayList?您可能需要使用clo

假设您有两个阵列:

    int[] a = {2, 3, 4};
    int[] b = {4, 5, 6};
如何将数组a设置为数组b并使它们保持不同的对象?就像我想做的那样:

    a = b; 
但这不起作用,因为它只生成“a”引用数组b。那么,在使两个数组保持独立对象的同时,将两个数组设置为相等的唯一方法是循环遍历一个数组的每个元素并将其设置为另一个吗


那么ArrayList呢?当一个ArrayList中有对象时,如何将其设置为另一个ArrayList?

您可能需要使用
clone

a = b.clone();
或者使用
arraycopy(对象源、int-sourcePosition、对象目标、int-destinationPosition、int-numberOfElements)


对于阵列,请查看:

  • )
对于
ArrayList

  • )

我认为这应该给你足够的指针,让你的作业取得进展。

提示:在两个数组中使用循环和公共索引。
Object.clone()
arrays.copyOf()
之间有什么区别?
System.arraycopy(b, 0, a, 0, b.length());