Win a copy of Event Streams in Action this week in the Java in General 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

Multi-Dimensional Arrays

 
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers;)

I have a question about Multi-Dimensional arrays.

When I have this Code, it compiles just fine:




But, in practice, it is... kind of BS, right?

Because the "second layer" of the objects Array is kind of... non existent,
I will not be able to ever use it right?

I know it's just an Exam Question, but for my understanding I think that there
is no real world scenario to ever declare such an array, right?;)

Kind regards
Florian
 
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:
Because the "second layer" of the objects Array is kind of... non existent,
I will not be able to ever use it right?



If you mean that the "middle" array will never hold any elements, or if the "5" size in the last array is moot, then yes.

However, it can still be used. The length is readable, so your code can check the length. And perhaps it is just an initial setup? meaning can always "resize" it later, by instantiating a new middle array type; like how the ArrayList collection does it.

Henry
 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

meaning can always "resize" it later, by instantiating a new middle array type;



you mean like this:



Couldn't find it on the ArrayList API, sry...
 
Henry Wong
author
Posts: 23835
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:
you mean like this:




Well, obviously, no -- as your example code will fail the cast at runtime.

... but ... I guess, yes -- in that you get the jist, in that the elements can be "resized" at runtime.

Florian Jedamzik wrote:
Couldn't find it on the ArrayList API, sry...



I wasn't referring to the API. I was talking about an implementation detail.

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:I know it's just an Exam Question, but for my understanding I think that there
is no real world scenario to ever declare such an array, right?;)


You are indeed correct! In a real world scenario you'll declare the array using a statement likeAnd then later on in the code you can initialize the arrays according to your needs. For example

But keep in mind that you can create an array without any elementsBoth are valid array declarations and will create an array without elements, so you can print the length attribute

Hope it helps!
Kind regards,
Roel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!