• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multidimensional Array Question  RSS feed

 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once again, Mala Gupta threw a curveball at me in the exam questions and doesn't explain it.


The above is "legal" in Java, which caught me off guard b/c I can't find a way to actually put values into it? Is that because the array has been set to a size of null or something? Since there wasn't anything inside the brackets, I initially thought that it wouldn't even compile, but it does. So I'm guessing that the value of the array is null for each element in ejg2? And that's why trying to print that array causes a compilation error?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like it creates String array of 2 elements and each element is of size 0.

So when you are trying to print any element of it, it will give ArrayIndexOutOfBoundException.
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:It looks like it creates String array of 2 elements and each element is of size 0.

So when you are trying to print any element of it, it will give ArrayIndexOutOfBoundException.


Exactly. Which makes me wonder why Java would even allow it then? Can you possibly do ANYTHING with that array or is it just a "trick question" kind of thing?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Newman wrote:Can you possibly do ANYTHING with that array or is it just a "trick question" kind of thing?

Yes there are things you can do with it, for example: If you have a method that returns an array (or array of arrays etc) and for a particular legal input parameter there is no value to return then it makes sense to return an empty array. If you couldn't declare an empty array you would have to return null or possibly an array of size 1 with a null value, either way the code that called the method would have to be followed by code to explicitly handle null values whereas an empty array doesn't need special case code.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah as Tony mentioned, it is good practice to return empty array instead of null to avoid NPE.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You create zero‑length arrays all the time. Whenever you write
java pack.Foo
you are passing the args array with no elements.

If you write the empty String "" (which is not null), that object incorporates an array of 0 chars.

The usual form of for loop for iterating an array
for (int i = 0; i < myArray.length; i++)...
handles arrays of any length, including 0. If you try that with a 0‑length array it will run exactly 0 times.
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Richard Newman wrote:Can you possibly do ANYTHING with that array or is it just a "trick question" kind of thing?

Yes there are things you can do with it, for example: If you have a method that returns an array (or array of arrays etc) and for a particular legal input parameter there is no value to return then it makes sense to return an empty array. If you couldn't declare an empty array you would have to return null or possibly an array of size 1 with a null value, either way the code that called the method would have to be followed by code to explicitly handle null values whereas an empty array doesn't need special case code.


That makes sense! Thanks guys!
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My original problem is still solved. Can someone explain this to me:


I'm am COMPLETELY lost on what's going on here.

How can ejgStr[], which is a single dimensional array refer to the multidimensional array?

Why does the multidimensional array have a [0] at the end of it?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's break that into a few lines.Does that make sense to you?
 
Richard Newman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Let's break that into a few lines.Does that make sense to you?


Yes, that makes sense. I guess the code was just such a clusterscrew that I couldn't see what was going on. Thank you sir!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!