This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

What is Object[][]

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is Object[][] mean? That is, what is it?


public static Object[][] getExcelData(String fileName, String sheetName)
{

}

Thanks
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means an array of arrays which are of type Object.

For example if you want to store the data in the cells in an excel spreadsheet you could use an array of arrays.
 
Marshal
Posts: 67273
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Docherty wrote:It means an array of arrays which are of type Object.


Note that Tony did not say "a 2-dimensional array". That's important.
 
Ranch Hand
Posts: 57
3
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fancypants term for this type of array is a jagged array. Use that to impress your friends.

From Wikipedia:

Wikipedia wrote:The array is called "jagged" because the elemental arrays can be of different sizes, producing rows of jagged edges when visualized as output.


Comes from this page.

To build on Bear's comment - While not directly related to Java, if you understand C# this stack overflow question has a lot of detailed information about how jagged arrays and two dimensional arrays differ.
 
M Wilson
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh!
Thank you much everybody!
 
Saloon Keeper
Posts: 3406
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, call me dumb, but why is this important?

If I need two indices to specify an element, then that makes it for me 2-dimensional.
I couldn't care less if several languages have several methods to store these things.
When it comes to collections, then we all say "program to the interfaces, the implementation
is unimportant", but when it comes to 2-dimensional arrays or arrays of arrays,
then suddenly many point to the fact that these are very different things, for they
are implemented very differently.

I don't get it.

Greetz,
Piet
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason it is so important is if you see Object[][] and assume it is a 2D array then occasionally you are going to either run into a IndexOutOfBoundsException or lose data because it may be that it isn't a representation of a 2D array.

To keep this simple lets call the first dimension 'row' and the second dimension 'column' well in a 2D array you know that every row will have the same number of columns but in an arrays of arrays this is not necessarily the case, row 1 may have more (or less) columns than the other rows or in fact every row may have a different number of columns.

 
Piet Souris
Saloon Keeper
Posts: 3406
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tony,

yes, I know this argument and it does indeed sound reasonably.
But the assumption made is that "a 2-dim array is per definition
rectangular". Now, that may have been valid for the BASICs from
the 80's, but since then we have moved on a little.

And, not to forget, even when we do consider it as an "array of
arrays", then IOOB errors are still very much possible!.

Well, never mind, I just didn't understand why this is considered
so important at this site.

But thanks for your reply!

Greetz,
Piet
 
Marshal
Posts: 65005
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fortunately, even C is moving away from supporting 2D arrays to arrays of arrays. In a real 2D array it is possible to dereference the wrong index.In a true 2D array where the entire array is stored in continguous memory it is possible to run that code and get it to print 4. In Java® which has never supported 2D arrays that code will throw an out of bounds Exception.
In a true 2D array it is impossible to change the size of one of the “row”s without corrupting the rest of the data.
 
Campbell Ritchie
Marshal
Posts: 65005
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Harris wrote:The fancypants term for this type of array is a jagged array. Use that to impress your friends. . . .

I think you only call it a jagged array when the elements are different sizes; if they are the same (??non‑zero??) size they are called rectangular arrays. With arrays of arrays you can change a jagged array to rectangular and vice versa.
 
Saloon Keeper
Posts: 10404
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:If I need two indices to specify an element, then that makes it for me 2-dimensional.


And this is exactly why it's not a 2D array. To get an element of an Object[][] a you pass it 1 argument: a[i]. The result is an array. The second indexer is not an operator on a, but on the element that the first indexer returned. This is a very important distinction.

Conversely, a proper 2D array Object[,] b requires two arguments: b[x,y].
 
Tim Harris
Ranch Hand
Posts: 57
3
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think you only call it a jagged array when the elements are different sizes; if they are the same (??non‑zero??) size they are called rectangular arrays. With arrays of arrays you can change a jagged array to rectangular and vice versa.



I was going off of what I understood, based off of the Wikipedia article I linked. From what I gathered, a jagged array could be rectangular, but it depends on the contents.
 
Piet Souris
Saloon Keeper
Posts: 3406
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Piet Souris wrote:If I need two indices to specify an element, then that makes it for me 2-dimensional.


And this is exactly why it's not a 2D array. (...)


As said: it is exactly why it is 2-D for me. I stated that the technical background was
irrelevant to me, so I don't understand why you give again some technical background.
But rest assured: in the years that I've been working with Java (about 5 years in my free
time) I have never encountered any problem whatsoever with my 2D arrays.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 65005
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't understand the technical background you won't understand why it always seems to work.
 
Stephan van Hulst
Saloon Keeper
Posts: 10404
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can disregard the implementation of an ArrayList if you use the List interface. That's because an ArrayList IS-A List. However, what we're trying to tell you that a 2D array IS-NOT-AN array of arrays. This has nothing to do with technical background or implementation. It's exactly the program to an interface problem! Now, this may not be a big deal if you're using them only as private fields or local variables, but you better do proper parameter checking if you're expecting a rectangular array in a method parameter!

Scratch that... you should almost never accept an array of any kind in a method signature anyway.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:
But rest assured: in the years that I've been working with Java (about 5 years in my free
time) I have never encountered any problem whatsoever with my 2D arrays.


Umm. That's a bit like saying crossing the road by shutting ones eyes and running is safe because I've done it for the last 5 years and I've never been hit by a car.

 
Piet Souris
Saloon Keeper
Posts: 3406
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's even worse than that. When we wrote an "actuarial
management information system" in the early 90's, in APL,
we saw that APL 2-D arrays were quite different things
than BASIC 2-D arrays, to say it mildly. Nevertheless
I still mentioned them 2-D. So it's been going well since,
well, a very long time.

The only serious problem I ever faced was when I had
just bought a C compiler for my pc, mid 90's. I went from my
BASIC chessboard%(7, 7) to int chessboard[7][7], and it took
me a very long time before I found out why that didn't work.
Of course, I knew about pointers in C, I gave the K&R 'C'
book a thorough read.

But I find it disappointing that all the smart guys around here
fail to see that for me, 2-D is just a naming convention, nothing
more and nothing less. I make no assumptions about the actual
structures used. But the good people here immediately jump to
the conclusion that my naming convention is wrong (!), that something
is only a 'proper' 2-D when you write it as A(x,y), that I do not
know about arrays of arrays and while doing so face terrible
risks ...

But in spite of all these comments, it still amazes me that people
around here react as if bitten by a snake, when some poor OP
mentions the 'unspeakable': 2-D

And that's all I have to say.

Greetz,
Piet
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:
But in spite of all these comments, it still amazes me that people
around here react as if bitten by a snake, when some poor OP
mentions the 'unspeakable': 2-D



Personally, it doesn't bother me -- as Java only supports arrays of arrays, and hence, I know what the OP is trying to say. On the other hand, there is nothing wrong with getting the terminology correct... being a Star Trek fan, I love the phrase "to boldly go", but that doesn't mean that the grammar is correct.


Also, as a side topic, what about languages that support both arrays of arrays *and* multidimensional arrays (such as C#)? Is it still just a naming convention?

Henry
 
Piet Souris
Saloon Keeper
Posts: 3406
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I promise, this will be my final reply.

Suppose, just for the sake of argument, that I have an interface, not accidently
called 'TwoD<E>', and that it has one method 'E retrieve(int x, int y)';

Now, let's say we have some classes that implement this interface. For instance,
I have the classes 'PietsStubbornAsAMule2DArrays', 'JavaArrays', 'APLIncredibleArrays',
'CsharpTrueMultinomialArrays', 'PlainOldBasicArrays', 'StephansProper2DArrays',
'StillToBeInventedArrays', to name a few.

Would I be allowed to call all these classes "TwoD"?

Now, one might shout "such an interface does not exist". It does however, in my mind.

Greetz,
Piet
 
Sheriff
Posts: 13549
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't really care much if these things are arrays of arrays or "improper" 2-D arrays, except when I do. Most of the time, I just think of them as two-dimensional or multi-dimensional arrays and that works just fine as a mental model. I get where you're coming from, Piet, but knowing that these are actually arrays of arrays does come in handy sometimes. I think folks around here just want to be technically correct even at the risk of appearing pedantic. To me, it's kind of the same difference as how Buckeye colors are Scarlet and Gray, not Red and Gray.
 
Stephan van Hulst
Saloon Keeper
Posts: 10404
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Would I be allowed to call all these classes "TwoD"?



Yes. The problem is that Java arrays can not implement such an interface by themselves. You can implement such an interface using Java arrays, but that would be composition rather than inheritance.

Another thing to consider: Is an Object[][][] a 3D array, an array of 2D arrays, a 2D array of arrays, or an array of arrays of arrays? If your answer is that it depends on the context, you're programming using 'weak types', much like using strings everywhere instead of proper strongly typed values. Strings Are Bad m'kay?

I understand what you're trying to say, but I think in our business it pays off being pedantic.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!