• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Array

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




options:
prints null
prints nullnull
compile-time error
Run-time exception

answer:run-time exception

ArrayIndexOutOfBoundsException at line:1

Explain
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because a3 is being converted to a max of A[3][2][1] when you are doing



so the utmost you can do is a3[2][1][0]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i compile the above program i am gettein error like..

A cannot be resolved to a type
could you explain this.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the above mentioned code your are tryiing to create and array of Type class A and thus you should declare a class named and thenn complie.

just add class a{} in your code and it will complie fine.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first part:
A[] a1=new A[1];
A[][] a2=new A[2][1];
A[][][] a3=new A[3][3][3];
System.out.println(a3[2][2][2]);

The arrays are created, and the default values (null) are put into their places.
Therefore the output is null.

Second part:

a1[0]=new A();
a2[0]=a2[1]=a1;
a3[0]=a3[1]=a3[2]=a2;
System.out.println(a3[2][2][2]);//1




a1 looks like this:
a1: { the new A() }

an array with only one element, length of the array is one.


When you say
a2[0]=a2[1]=a1;

a2 looks like:
a2: { a1, a1 }
Now, a2 no longer has the length of three because it has been reassigned.


and finally
a3[0]=a3[1]=a3[2]=a2;

a3 looks like:
a3: { a2, a2, a2 }



But when you want to have:
System.out.println(a3[2][2][2]);//1

Starting from left to right:
a3[2] points to a2 (all three elements of a3 are a2).

Therefore
a3[2] [2] is the same as
a2[2]
but a2 has only a2[0] and a2[1] (see bold line above), therefore the exception.


Perhaps draw the arrays on a sheet of paper.


Yours,
Bu.
 
suresh mulagala
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swathi try this...

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was a superbb explanation

by Mr Burkhard Hassel !!
 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Burkhard for explaining so briefly....

Thank you for helping me understand many topics as you have replied to most of my posts...
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic