简体   繁体   English

为什么打印3 ln 8 4?

[英]Why does this print 3 ln 8 4?

boolean[][] values = new boolean[3][4];
System.out.println(values.length);
values[2] = new boolean[8];
System.out.println(values[2].length + " " + values[0].length);

This is a multiple choice question that I'm having trouble with. 这是我遇到的多选题。 The answer is supposed to be: 答案应该是:

3
8 4

but I thought that an array's size cannot be changed once it is created. 但是我认为一旦创建数组就无法更改大小。 Any explanations would be much appreciated. 任何解释将不胜感激。

You are not changing the length of an array, you are creating a new one of a different length. 您没有更改数组的长度,而是创建了另一个不同长度的数组。

values[2] = new boolean[4]; 
assert values[2].length == 4;

values[2] = new boolean[8]; 
assert values[2].length == 8;

is just like writing 就像写作

boolean[] values2 = new boolean[4]; 
assert values2.length == 4;

values2 = new boolean[8]; 
assert values2.length == 8;

Note: a boolean[] variable is a reference to an array. 注意: boolean[]变量是对数组的引用 It is not the array object so when you change this reference you point to a different object. 它不是数组对象,因此当您更改此引用时,您将指向另一个对象。

values = new boolean[3][4]

... creates an array of length 3, pointed to by a variable called values . ...创建一个长度为3的数组,由一个名为values的变量指向。 Each of the three elements in values points to an array of length 4. values中的三个元素均指向长度为4的数组。

So: 所以:

System.out.println(values.length);

... prints 3. ...打印3。

values[2] = new boolean[8];

... creates a new array of length 8, and makes element 2 of values point to it. ...创建一个新的长度为8的数组,并使values元素2指向它。

The array that used to be element 2 of values no longer has a reference -- it's lost (if the JVM stays around long enough, it will be cleared away by garbage collection). 曾经是值元素2的数组不再具有引用-它丢失了(如果JVM停留足够长的时间,它将被垃圾回收清除)。

values[0] is still the 4 element array created at the start. values[0]仍然是开始时创建的4元素数组。 values[2] is the newly created array of length 8. values[2]是新创建的长度为8的数组。

In Java, two-dimensional arrays are actually arrays of arrays, not a block of bytes that's divided into rows, as it is in some other languages. 在Java中,二维数组实际上是数组的数组,而不是象某些其他语言那样分成行的字节块。

When you declare an array as 当您将数组声明为

boolean[][] values = new boolean[3][4];

It's basically just a shorthand to writing: 这基本上只是编写的简写:

boolean[][] values = {
     new boolean[4],
     new boolean[4],
     new boolean[4]
};

So you have an array whose elements are arrays of booleans. 因此,您有一个数组,其元素是布尔数组。

There is nothing stopping you from changing one of the entries in this array. 没有什么可以阻止您更改此数组中的一项。 You have a first array, a second array, and a third array, and you are just replacing the third. 您有第一个数组,第二个数组和第三个数组,而您只是要替换第三个。

Thus, it is not changing the size of the array, because the declaration new boolean[3][4] only sets a fixed size for the first dimension, and for the initial values (arrays) in it. 因此,它不会更改数组的大小,因为声明new boolean[3][4]仅为第一维及其初始值(数组)设置固定大小。 But you can replace those initial values with new values which have a different size if you wish - as long as you don't try to change the size of the main array. 但是,您可以根据需要用具有不同大小的新值替换这些初始值-只要您不尝试更改主数组的大小即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM