Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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
Java字符数组最大容量_Java - Fatal编程技术网

Java字符数组最大容量

Java字符数组最大容量,java,Java,我试图用Java编写一些围绕字符数组的代码,并提出一些问题,从下面的第一个问题开始。如果可能的话,我更喜欢使用Java8,避免使用循环。请帮忙,谢谢你 问题1:打印字符数组的最大容量 // Create a character array that can hold a max of 10 elements and copy over the contents from another character array. char[] charArr1 = {'A','B','C'}; cha

我试图用Java编写一些围绕字符数组的代码,并提出一些问题,从下面的第一个问题开始。如果可能的话,我更喜欢使用Java8,避免使用循环。请帮忙,谢谢你

问题1:打印字符数组的最大容量

// Create a character array that can hold a max of 10 elements and copy 
over the contents from another character array.

char[] charArr1 = {'A','B','C'};
char[] charArr2 = new char[10];
charArr2 = charArr1.clone();

// I wanted the result below to be 10, but the output was 3.

System.out.println(charArr2.length);
在这里:

你假设:

我希望下面的结果是10,但输出是3

你的问题是克隆人并不像你想象的那样。您认为它将一个数组的内容复制到另一个数组中。但事实并非如此。相反:

当对数组调用clone方法时,它返回对新数组的引用,该数组包含或引用与源数组相同的元素

从这个

如果要保留使用新字符创建的数组[10];例如,您必须使用。该调用保留目标数组,从某个源数组复制值。

charArr2=。。。是一个赋值,所以新字符[10]消失了
char[] charArr2 = new char[10];
charArr2 = charArr1.clone();