• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help in array

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A13 {}
class A14 {
public static void main(String[] arg) {
A13[] a1 = new A13[1]; // 1
A13[][] a2 = new A13[2][1]; // 2
A13[][][] a3 = new A13[3][3][3]; // 3
System.out.print(a3[2][2][2]); // 4
a1[0] = new A13(); // 5
a2[0] = a2[1] = a1; // 6
a3[0] = a3[1] = a3[2] = a2; // 7
System.out.print(a3[2][2][2]); // 8
}}

gives run time error..
but
class A11 {public String toString() {return "A11";}}
class A12 {
public static void main(String[] arg) {
A11[] a1 = new A11[1]; // 1
A11[][] a2 = new A11[2][]; // 2
A11[][][] a3 = new A11[3][][]; // 3
a1[0] = new A11(); // 4
a2[0] = a2[1] = a1; // 5
a3[0] = a3[1] = a3[2] = a2; // 6
System.out.print(a3[2][1][0]); // 7
}}

this works fine...how?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mathi,
The first one will fail for an arrayindexoutofbounds exception as
a3[2][2][2] will cause it.
array a2 is a 2x1 object and [2] refers to third array object and it fails.

The second is perfectly correct as it refers to [2][1][0].
if u change it to [2][2][2] it will fail with the same exception.

Thanks,
Vijay
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use tags to preserve formatting, if any, in code.
If you get a runtime exception tell us what it is.

a3[2] references an array a2 with two elements each of which are references to an array with one element. You are referencing the 3rd argument of a two element array, so you are getting an IndexOutOfBoundsException.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A13 {}
class A14 {
public static void main(String[] arg) {
A13[] a1 = new A13[1]; // 1

A13[][] a2 = new A13[2][1]; // 2

A13[][][] a3 = new A13[3][3][3]; // 3
System.out.print(a3[2][2][2]); // 4
a1[0] = new A13(); // 5
a2[0] = a2[1] = a1; // 6
a3[0] = a3[1] = a3[2] = a2; // 7
System.out.print(a3[2][2][2]); // 8
}}

gives run time error..
but
class A11 {public String toString() {return "A11";}}
class A12 {
public static void main(String[] arg) {
A11[] a1 = new A11[1]; // 1

A11[][] a2 = new A11[2][]; // 2

A11[][][] a3 = new A11[3][][]; // 3
a1[0] = new A11(); // 4
a2[0] = a2[1] = a1; // 5
a3[0] = a3[1] = a3[2] = a2; // 6
System.out.print(a3[2][1][0]); // 7
}}


I m confused in these bold lines ...
vijay can u explain it in detail.. regrd size of arrays...?
In
A13[][] a2 = new A13[2][1]; // 2
&
System.out.print(a3[2][2][2]); // 8

Also In
A11[][] a2 = new A11[2][]; // 2
System.out.print(a3[2][1][0]); // 7

Thanx
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kavita,

In Line 2:

A13[][] a2 = new A13[2][1]; // 2

a2 is an array of arrays of A13 objects. It contains two elements a2[0] and a2[1]. Both a2[0] and a2[1] are arrays of A13 objects and each contain only one element. In the case of a2[0], the element is a2[0][0]. In the case of a2[1], it is a2[1][0].

In line 8:
a3 is an array of arrays of arrays of A13 objects. The dimensions are 3 x 3 x 3, in the beginning. But later, the dimensions change to 3 x 2 x 1, due to reassignments of the top level elements. When dereferencing with a3[2][2][2], the index 2 is trying to access the 3rd element (Java arrays are 0-based). There is no 3rd element for the second and third level indices, as we said the array gets changed to 3 x 2 x 1. This causes an IndexOutOfBoundsException, as Barry explained very well.

Next problem:

A11[][] a2 = new A11[2][]; // 2

a2 is an array of arrays of A11 objects. It has two elements a2[0] and a2[1]. Both are assigned to null because the second dimension was not specified. Later on, in line 5, the nulls are overwritten with a1, which is an array of A11 objects, containing only one element.


System.out.print(a3[2][1][0]); // 7

a3 is an array of arrays of arrays of A11 objects. It has 3 elements, each is an array of arrays of A11 objects. These are all set to null in line 3. Later, in line 6, all three of these elements( a3[0], a3[1], a3[2] ) are assigned to a2, which I just described above.

In the end, the three elements of the A11[][][] array all refer to the same object. This object is a A11[][] array with two elements. Both elements of the A11[][] array refer to the same object. This is an A11[] array with a single element. This single element is an A11 object (created in line 4). This means that every (lowest-level) A11 element in the A11[][][] array is actually the same A11 object.

It helps to draw diagrams when figuring this stuff out. Look at page 31 in Sierra & Bates SCJP 1.4 Study Guide, for example. Visualizing the problem makes it a lot easier to understand.

I hope this helps.
[ July 11, 2006: Message edited by: Douglas Chorpita ]
 
Kavita Kale
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Douglas Chorpita
i understood it..whatever u explain.. now i will draw the diagram..accorading to katthy's book..so that it clears my concept very perfecly... Thanx for cheering
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic