2. int[][] iaa = new int[3][];
should be
2. int[][] iaa = new int[2][];
as illustrated in following paragraph where
Example 1 has [2][3], while here, it is [2][].
Moderator: admin
Example 1 has [2][3], while here, it is [2][].
should be[2] implies
that you want to store two references. In other of words, the length of your array
(which is of type array of ints) is 2.
?[3] implies
that you want to store three references. In other of words, the length of your array
(which is of type array of ints) is 3.
I think the 3rd create should be care? (I really like this explanation. This helps me tremendously compared to the previous explanation.)explanation to why int[][2] isn't valid has also been updated.
Sorry, I did not understand what you mean by this? Could you please elaborate a bit?Username987654 wrote: ↑Sun Dec 02, 2018 10:52 pmI think the 3rd create should be care? (I really like this explanation. This helps me tremendously compared to the previous explanation.)explanation to why int[][2] isn't valid has also been updated.
Correct. Should be fixed.Username987654 wrote: ↑Sun Dec 02, 2018 11:14 pmia should be iaa starting with paragraph "In this figure" throughout the paragraph "But in case of an array of array of ints". (I just now was able to figure out why I was not understanding this text as written.)
admin wrote: ↑Sun Dec 02, 2018 11:37 pmSorry, I did not understand what you mean by this? Could you please elaborate a bit?Username987654 wrote: ↑Sun Dec 02, 2018 10:52 pmI think the 3rd create should be care? (I really like this explanation. This helps me tremendously compared to the previous explanation.)explanation to why int[][2] isn't valid has also been updated.
should likely beYou cannot, however, leave out the size of a higher dimension if you want to specify the size of a lower dimension. For example, you cannot do new int[][2]; The reason is simple - new int[][2] tries to create an array of int[2] objects. But it it does not tell the JVM how many int[2] objects you want to store. Without this information, the JVM has no idea how much space it needs to allocate for this array. On the other hand, new int[2][] is fine because now, you are telling the JVM that you want to create an array of length 2. In this case, the JVM is clear that it needs to allocate space to store 2 references. Remember that the size of a reference doesn't depend on the length of the array to which it points. So, the JVM doesn't create about the length of the arrays to which these two references will refer. It simply allocates space to store 2 references.
?You cannot, however, leave out the size of a higher dimension if you want to specify the size of a lower dimension. For example, you cannot do new int[][2]; The reason is simple - new int[][2] tries to create an array of int[2] objects. But it it does not tell the JVM how many int[2] objects you want to store. Without this information, the JVM has no idea how much space it needs to allocate for this array. On the other hand, new int[2][] is fine because now, you are telling the JVM that you want to create an array of length 2. In this case, the JVM is clear that it needs to allocate space to store 2 references. Remember that the size of a reference doesn't depend on the length of the array to which it points. So, the JVM doesn't care about the length of the arrays to which these two references will refer. It simply allocates space to store 2 references.
ia should be iaa?iaa[0] = new int[2]; //ia[0] points to an array of ints of length 2
iaa[1] = new int[3]; //ia[1] points to an array of ints of length 3
should beIn both the cases, iaa refers to an array of length 1.
?In both the cases, iaa refers to an array of array of ints. The length of the array is 1.
I thought that was the intent. I (wrongfully?) separated those sentences: As a point of clarification, is it consistently stated with other parts of the text? If not, could it be a source of confusion to some readers? Number 4, immediately under it, mentions Object[] obj[] as "an array of array of objects". The last lines in Section 4.3.2 go into specific detail here as well. In other words, iaa[0] is the real single dimensional array that refers to a single dimensional array of length two. A single dimensional array of ints cannot contain an array. Correct?admin wrote: ↑Fri Nov 22, 2019 9:51 amNo, it is fine because the point that it is trying to make is that iaa refers to an array of length one. It is true that the element of that array itself is an array but that is not relevant in this sentence. It is made clear in the next sentence, "The first and only element in this array refers to an array of ints of length 2."
You need to be very clear about this concept because it gets confusing very quickly
So, yes, it is consistently stated that a multi dimensional array in Java is actually just an array of arrays. In other words, an "array of arrays" is an array!The phrase multidimensional array brings a picture of a matrix to mind. But it is important to understand that Java doesn’t have matrix kind of multidimensional arrays. What Java has is arrays whose elements themselves can be arrays. Recall that, in Java, every array object is an object of a
specific class. For example, the class of an array of ints is [I. Now, what if you want to have an array of objects of this class. In other words, an array of “array of ints”. You can declare it like this:
int[][] iaa;
There is nothing like real or fake single dimensional array. Actually, if it helps your understanding, you can safely say that Java doesn't have the concept of single or multi dimensional arrays. Java has, just, arrays. If the elements of an array are themselves arrays, then we call them multi-dimensional arrays from human perspective. From the language perspective, it is just an array like any other array.iaa[0] is the real single dimensional array that refers to a single dimensional array of length two
Users browsing this forum: Bing [Bot] and 120 guests