This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes reassigning new references to previously defined array variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reassigning new references to previously defined array variables" Watch "reassigning new references to previously defined array variables" New topic
Author

reassigning new references to previously defined array variables

Leo Velazquez
Greenhorn

Joined: Apr 27, 2006
Posts: 13
In multi-dimensional arrays the second and on dimensions which are arrays
themselves can be swapped for other arrays that are not necessarily of
the same lengths. I get the impression that this is not possible with either
one-dimensional arrays or the first dimension of multidimensional arrays
although none of the four tutorial books I have will simply state this.

int [ ] arr_int1 = new int [5];
int [ ] arr_int2 = new int [4];
int [ ] arr_int3 = new int [6];

//Which of these two following statements are illegal?
arr_int1 = arr_int2;
arr_int1 = arr_int3;

Also

arr_multdim1 int [ ] [ ] [ ] = new int [3] [4] [6];
arr_multdim2 int [ ] [ ] [ ] = new int [3] [7] [20];
arr_multdim3 int [ ] [ ] [ ] = new int [4] [4] [6];

//Which of these two following statements are illegal?

arr_multdim1 = arr_multdim2;
arr_multdim1 = arr_multdim3;

Would appreciate the help. Thanx in advance.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
None of the above. If you have a two dimension array then it's an array of arrays. The length of the arrays isn't guaranteed to be anything specific, it's not even guaranteed to be the same for each array.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
The best way to find the answer is to run the code yourself, after you work throught the code and see what you think it should be. Note any discrepencies, try to find out why, and bring specific questions.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
If you have a two-dimensional array, then you aren't using Java. Java and multi-dimensional arrays are mutually exclusive things.


Tony Morris
Java Q&A (FAQ, Trivia)
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Then why does the JLS specifically refer to an array of array as a multidimensional array at least once?

You are nitpicking beyond all reason. int[][] myInts will behave exactly like a multidemensional array,ie you access elements and move around the multidimensional array same as you would in any other language that supports multi-dimensional arrays. Whether or not it is different in memory is completely irrelevant to the Java programmer, as such things are abstracted away and only behavior matters in most cases.

For all intents and purposes they are equivilent terms.

To get back to the original question, what happens with this bit of code?

String [] files;
File myFile = new File(fileName);
...

files = myFile.list();//returns the names of files and directories in the directory passed in the File constructor and returns a String[].

files is never instantiated, so of course has no size, yet this is valid code. So would anything be different if this happened:

String [] files = new int[5];
File myFile = new File(fileName);
...

files = myFile.list();//might return more or less Strings then 5

Likewise if it were a reference to an object:

Integer myInt = new Integer(5);
Integer myInt2 = new Integer(768);

...

myInt = myInt2:

Would the compiler complain about this?

basically, what is a reference and what does it care about?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Then why does the JLS specifically refer to an array of array as a multidimensional array at least once?

Because it was written by Sun.

You are nitpicking beyond all reason.

If it is "beyond all reason", perhaps you'd like to explain why all the newcomers who continually struggle to conceptualise and therefore (attempt to) use the imaginary multi-dimensional array - because it does not exist is my best explanation so far?

C# has both multi-dimensional arrays and arrays of arrays - how do you explain that? COBOL has tables/multi-dimensional arrays. Programming language theory has held and still holds that Java does not have multi-dimensional arrays. You can write whatever you like in the non-authoritative (but bible-like according to the masses) JLS. On that note, ever tried to implement that thing? Those who do (myself included once upon an evil time) do not refer to the JLS - not for a second - wanna know why? The RI is authoritative and nothing more.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I was under the impression that Java had "jagged" arrays which were a form of "multidimensional" array. I could be horribly mistaken.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Java has single-dimension arrays, and uses arrays of arrays to model arrays of higher dimensions. Whether or not the terminology is accurate in a broader context, this is what is meant by "multi-dimensional array" in Java.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Rusty]: Then why does the JLS specifically refer to an array of array as a multidimensional array at least once?

Note that the first time they use the term, it's in this sentence: "The language supports arrays of arrays, rather than multidimensional arrays." Subsequent uses manage to contradict this, unfortunately. Note also that the JLS never formally defined this term anyway. They just make some vague references and let you guess what it means, for better or worse.

[Ken]: I was under the impression that Java had "jagged" arrays which were a form of "multidimensional" array. I could be horribly mistaken.

I guess it depends on how much "kind of" is allowed to corrupt the original meaning of the phrase "multidimensional array". In languages which have both true multidimensional arrays and jagged arrays, it's important to distinguish between the two. It may seem less imperative in Java, since we can usually just mentally translate that anytime someone says "multidimensional" they really meant jagged, since that's the only option in Java. Still it seems preferable to keep the terms separate. Sun unfortunately never picked a better general term to replace "multidimensional" it their discussions. Of course "array of arrays" works fine if there are only two levels, but it's a pain if you want to talk about arrays of arrays, arrays of arrays of arrays, arrays of arrays of arrays of arrays, etc.

Incidentally I prefer the term "composite arrays" rather than "jagged", because the latter seems to imply that the structure actually is jagged (having different lengths) when in fact that is frequently not the case (in Java anyway). Unfortunately "composite" never caught on for arrays, and now C# seems to have entrenched "jagged" instead. Oh well; I can live with it.
[ June 30, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Then my impression was wrong. I know in some languages like C# they distinguish between the two because it has both. I wasn't aware of whether or not a jagged array was conventionally considered a form of a multidimensional array. As I've said before, terminology is a weak point for me so I always seek to clarify misconceptions and this was one of them. So despite the fact that we know we're referring to a jagged array in the context of Java, it's not really a multidimensional array? Okay, thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Ken]: So despite the fact that we know we're referring to a jagged array in the context of Java, it's not really a multidimensional array?

That's my take. In the context of Java, if someone says "multidimensional array" I figure they mean a jagged array. But the moment someone adds "really" I figure they're referring to the distinction used outside Java, in which a jagged array and a multidimensional array are not the same thing.
Leo Velazquez
Greenhorn

Joined: Apr 27, 2006
Posts: 13
Oh I see. So in Java all array references are non-constant, reassignable pointers to any length of array of their declared type.
Leo Velazquez
Greenhorn

Joined: Apr 27, 2006
Posts: 13
But then why does the first dimension of a multi-dimensional array have to
be indicated?
Is this why we need an anonymous array creation to re-initialize it?
Leo Velazquez
Greenhorn

Joined: Apr 27, 2006
Posts: 13
Is the first dimension chiseled in stone and therefore we need an anoymous
array assignment to revise it or is the array reference itself just another
array reference re-assignable with any length in its first dimension?
 
wood burning stoves
 
subject: reassigning new references to previously defined array variables
 
Similar Threads
Arrays in Java
Mock Exam quesions
Arrays
Doubt in Array Declaration
Doubt in Arrays