Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Array initialization

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[][] a = new int[1][1];
int[] b = new int[4];
a[0] = b;
System.out.println(a[0][2]);

a[0] can hold a 1-D array with size 1 . but it holding 1-D array with size 4.

please explain why it is not throwing ArrayIndexOutofException.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a[0] contains only the reference of array b not whole array.
that's y it's correct.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have assigned int[4] to a[0]. So you can use a[0][0] up to a[0][3].
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here the array is created with int[][] a = new [1][1].
as per declartion, a[0] can hold a 1-D array with size 1.

my doubt is how a[0] is able to hold a 1-D array with size 4.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As this is only a reference that changes, the array itself can still grow at runtime.
[ October 24, 2006: Message edited by: Bram Maes ]
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[][] a = new int[1][1];
a[0][2] = 12; this will throw ArrayIndexOutofBoundException.

i think array will not grow in run time.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the array won't grow. If you are used to C/C++ and the way that C/C++ treats arrays, I think this is the cause for this confusion.

In C, a 2D array is not a double pointer. Once defined as array[x][y], it will always have x rows and y columns. Besides this, assigning a value to array[k] is simply illegal.
This is closely related to the way C treats arrays internally, the way arrays are stored into machine memory.

A Java 2D-array does not behave like a C 2D. It is much more like double pointer or a pointer to an array in C: int * array[]. This means that array[0] can have X elements and array[1] can have Y elements.

The 2D array in java is just an array of 1D-arrays of any sizes. It's just a special case that initialization like new int[x][y] creates x 1D-arrays, all with y columns. The 1D-array elements can be reassigned and you may very well have array[k] = null !

To go back to the C comparison, think of the argv argument for the main() function, which is defined like char ** or char * argv[], meaning that the arguments may very well have different lengthes. This is the closest representation of a Java array.

I hope this answers your question.
[ October 24, 2006: Message edited by: Costi Ciudatu ]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short I'd just say you initially assigned an array of length 1 to a[0], you then re-assigned a different array (of lenth 4) to a[0], so ofcourse the System.out.println(a[0][2]) has no problem.

"a[0] can hold a 1-D array with size 1 . but it holding 1-D array with size 4." - that is incorrect. a[0] can hold ANY 1-d int array of any size. It initially has an array of size 1, it then has a different one of size 4.
 
Costi Ciudatu
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, exactly.
To speak java now, in int[][] you hold an array of references to int[] objects. In initialization like arr = new int[2][2] you make arr[0] = new int[2]; arr[1] = new int[2]. When you do: arr[1] = new int[4], the former value of arr[1] (which was an int[2]) gets lost (eligible for GC), and arr[1] now holds a reference to the new object that was just created (which is an int[4]).
The references stored in int[][] are to int[] objects of any size not just to a particular int[X].
[ October 24, 2006: Message edited by: Costi Ciudatu ]
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[][] a = new int[x][y].
The y size can be increased in run time.
this means that array is growable like collection.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sentil kumar:
int[][] a = new int[x][y].
The y size can be increased in run time.
this means that array is growable like collection.



No... the array is not growing.

When you assign an array reference to another array value. The reference merely points to the new object, and the old one gets dereferenced.

2D arrays are merely arrays of arrays, so you can not only change the values in the arrays that hold the values, but also the array that hold the arrays too.

Henry
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sentil there is a massive difference between assignment and modification.

The size of arrays can not change, however a new array can be assigned to a variable.

E.g


The underlying array that reference variable 'a' refers to was not modified, instead the variable 'a' was changed to refer to a different (newly instantiated) array object, which is of size 10.

Apply the same to the two dimentional array and this should become clear.
 
sentil kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my doubt is got cleared.
Thanks a lot...
 
I think she's lovely. It's this tiny ad that called her crazy:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic