Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resize the multi dimension array

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have this question,
say if I declare a 2D array for example;
int arr[][] = new int[2][2];

and declare a single dimensional array;
int x[] = new int[3];

so my question is that can I assign like this;

arr[1] = x;

Upto to my knowledge the size of the array once declared can't grow. But here it's not happening like that?
Could you please explain this to me,
Thank you in adavance.....
 
Daniel Botelho
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so my question is that can I assign like this;

arr[1] = x;
I think so... :roll:

I've tryied this code and it does that:


Best regards,
[PT]Devilishly
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that this is a multidimensional array has nothing to do with it.
Think of what you are doing you are declaring arr[1] (and arr[0]) to be a refence to a new array of ints.
You then assign this reference to the array of 2 ints.
When you create a new array of 3 ints you assign the same reference to this array.
These are two seperate arrays. You are not changing the size of either and you are right you cannot!
All you are doing is taking the reference from one array and assigning it to another!
Hope this helps,
Barry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic