• 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 Question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ArrayEx
{
static public void main(String ar[])
{
int a[]={2,5,7,1};
int b[]={5,8,3,2};

System.out.println(a[(a=b)[3]]);
System.out.println(a[(a=b)[2]]);
System.out.println(a[(a=b)[1]]);



}
}

The output:
C:\>java ArrayEx
7
2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at ArrayEx.main(ArrayEx.java:10)

i am not able to get the flow of this particular problem can anyone explain it ?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi RaviRaj,

the code is equivalent to something like this:
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ravi

(a[(a=b)[3]]);

you are
assigning b's 3rd element to a now it is becoming a[2]= 7
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since because at runtime you assign the array "b" to "a" and taking the new array (b)'s index value to be used as the index to the original array "a", one of the values present in the "b" array (8) is causing an ArrayIndexOutOfBoundsException.

HtH.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in the output especially in the second SOP.

As per the dynamic array assignment (b to a) before accessing the index of original array "a", the second statement should give the value "1" because b[2] is 3 and when it becomes an index of "a", a[3] is 1.

It gives the correct value "1" in the output when its being run alone.

But when its being run along with the first SOP, it gives the value "2".


Why is it so?
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After executing the first statement, a also references the array b. Thats why the output is 2 instead of 1.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats correct, probably thats the reason!

Thanks Al Mamun!
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic