• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with multidimensional array with size only in the first square  RSS feed

 
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
my first post here but I appreciate this forum since few months ago, I like it very much because I see so many people that they have no problem helping the less experienced (usually in forums in my language newbie are ignored).

I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:



the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!

Could someone please explain it? Thank you so much.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emanuele Mazzante wrote:
I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:



the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!

Could someone please explain it? Thank you so much.



Java doesn't really support true multidimensional array -- it just looks like it does. What Java supports are arrays of arrays, so when you allocate a two dimensional array like so...



It is creating an array of int arrays of size two. And also looping through that array to create two int arrays of size three, which are referred to by the array of array elements.

So, when you do this...



You are creating an array of int arrays, whose elements are null. You will need to allocate the individual int array elements later, if you want to store any ints through.

Henry

 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are like boxes. You can put stuff in them. One of the things you can put in them is...other boxes. so when you have this:

new int[2][];

you're saying "i want a box that can hold two boxes. Those two boxes will hold integers, but I don't know how many integers those boxes will hold."

So later, you can create an array of ints of ANY size, and put them in your original box.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have got good answers, but

Welcome to the Ranch
 
Emanuele Mazzante
Ranch Hand
Posts: 62
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helping me. Now I understand. I was looking for a way to do something like this:

multiArr[1][]=5;

of course it's wrong so I thought that instruction code int[][] multiArr = new int[2][] was meaningless. Now thanks to you I understand my logic error and I can do something like this:

multiArr[0]=new int[3];
multiArr[1]=new int[2];

thanks again, thanks for your patience and thanks for the welcome!
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or:-Or:-
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

Wouldn't that mean that multArray is int[][][] ?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! Yes, it would. Well done picking up my mistake. It should readI even compiled it to check and then made a mistake with what I posted here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!