No, it isn't, because Java® doesn't support 2D arrays. This is what the memory looks like if you have a 2D array:-But what you actually have is this:-In the case of a 2D array, which I had to deal with in older C implementations, all the elements are consecutive and it was possible to write such horrors as myArray[0][3]. In Java®, the array contains other arrays and their contents are in different memory locations. That is an array of arrays, and it is the only kind of array nesting you can have in Java®. Anybody telling you that is a 2D array is mistaken.Adam Chalkley wrote:. . . firstly it's a 2d array . . .
Yes and no. If you declare new int[4][3], you get a structure like this:-. . . because arrays are filled with 0s or something else non‑specific until you fill them with something else. Note it has the twelve memory locations in the same arrangement as before, which is the similar bit. This is why I like the array initialisers as inwould this be similar to declaring the array as
? . . . thanks
That is one of the advantages arrays of arrays have over 2D arrays. Also you can reassign them so as to change the sizes of the individual arrays, so the whole thing changes shape.Fred Kleinschmidt wrote:. . . it is not necessary for the array to be square; it is possible for each array to have a different length
Liutauras Vilda wrote:I know some people thinking - we say way too often that Java does not have 2D arrays. Since I never programmed in a language/-s which truly support 2D arrays, I don't know the real strong argument about that or difference. People with more experience could tell you more.
Aaaaaaaaaaaaaaaaaaaaaaaaaah! Nice!Stephan van Hulst wrote:. . . that this isn't *really* an array of arrays, but rather an array of pointers.
Campbell Ritchie wrote:It used to be possible for me to write this sort of thing in C:-Line 3: Remember that an array is equivalent to a pointer in C. Prints address.
Campbell Ritchie wrote:
That is one of the advantages arrays of arrays have over 2D arrays. Also you can reassign them so as to change the sizes of the individual arrays, so the whole thing changes shape.Fred Kleinschmidt wrote:. . . it is not necessary for the array to be square; it is possible for each array to have a different length
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Adam Chalkley wrote:Yes I remember reading in a c++ book that in c and c++ multi dimensional arrays are contiguous in memory,so in Java this is not the case?
That's a pleasureAdam Chalkley wrote:. . . thanks guys for all the replies
So will everybody else involved in this thread, or reading it. That is one of the reasons we post at all.I'm going to learn a lot from this thread =)
I think Stephan has already answered that. There ain't no such thing as a multidimensional array in Java® at all. Look at the 3rd post in this discussion, my first one. You can see in the second code block that I have the numbers on different rows. The first row shows the references to the individual arrays all on one line, then you have four lines with the included arrays. I wrote that to represent the fact that although each array has its elements in consecutive and contiguous memory locations, they are on different rows and may be separated in memory.Yes I remember reading in a c++ book that in c and c++ multi dimensional arrays are contiguous in memory,so in Java this is not the case?
Stephan van Hulst wrote:In Java is an array of arrays
Liutauras Vilda wrote:
Stephan van Hulst wrote:In Java is an array of arrays
And I think it is important to emphasize, that it is a singular array, rather than arrays of arrays. Because there is always one array A.length, which possibly holds more arrays, which possibly hold more arrays.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
There are three kinds of actuaries: those who can count, and those who can't.