Campbell Ritchie wrote:
Who has taught you about 2D arrays, because they are mistaken. There ain't no such thing in Java®. What you have written is an array of arrays.
Tyson Lindner wrote:
Campbell Ritchie wrote:
Who has taught you about 2D arrays, because they are mistaken. There ain't no such thing in Java®. What you have written is an array of arrays.
What's the difference? I could have sworn we've called this type of object a 2D array before in this forum.
"Il y a peu de choses qui me soient impossibles..."
Campbell Ritchie wrote:
Java® does not support 2D arrays;
C does.
In C you may find that all the elements of all the “rows” follow one another in memory.
So in a C array like… it is possible to have the numbers in memory like this:-In a Java® ordinary (=1D) array…you would get the same sequence of numbers in memory (probably plus a 9 for length of array), but for a Java® array of arrays you would get something like this instead… where the 3 means it is a 3‑element array and the 3 hex numbers are pointers whereby the constituent arrays can be found. (They might be pointers to pointers, which you call handles. On a 64‑bit machine the pointers may go up in 8s rather than in 4s.)
Stevens!There are two differences in practice:
1: In some C implementations it is possible to go over the end of an array. It may be possible to write myArray[3] in C and get the program to print 4. You cannot go over the end of one of the constituent arrays in Java® because you will suffer an exception. Exactly what Stephens
Miller showed earlier.
2: The elements of an array of arrays may be different lengths. The elements of a 2D array are usually always the same length. SM asks whether that is an advantage; I think it is an advantage, even if you use it only rarely. If you write the first code line in Java® you can reassign part of the array:
"Il y a peu de choses qui me soient impossibles..."
If you create the 2D array I showed earlier in C and then writeStevens Miller wrote: . . .
But you can only tell the difference because C permits pointer arithmetic. If you don't use that, your C code could as well be Java code.
But we are now moving out of the realm of simple arrays. You are either creating an array of arrays specifically or another data structure.. . . Come to think of it, none of those arrays must have the same size as any other, though you'd have to track the sizes externally, but that would only take a simple data structure. . . .
It is of course a virtueJava hides all that, which is a virtue/drawback that is hotly defended/derided by a lot of people. . . .
I haven't got the time to go through this thread at the moment, but if you can work out how to split it, feel free to do soI'm sure this discussion is extremely helpful to our Greenhorn OP
.
Sorry for the mistake.Stevens Miller wrote: . . . Stevens! . . .