• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Array index

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the source is

inquistion john meyers's SCJP 5 mock exam

but really i have a question regarding quoting the questions?

i prepared for the exam for along time and each time when i found any diffcult question for me , i copied it in some word file till i can ask about it
so i have a lot questions that i don't remember from where i had got it and what its source??

so that matter may make me don't quote some of the questions i send.

 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code executes this way:

first iteration:
i = 1, x[1] = 0, array becomes: x = (1, 0 , 3, 4)
second iteration:
i = 0, x[0] = 0, array becomes: x = (0, 0, 3, 4)
third iteration:
i = 3, x[3] = 0, array becomes: x = (0, 0, 3, 0)
fourth iteration:
i = 0, x[0] = 0, array becomes: x = (0, 0, 3, 0)

final answer: x = (0, 0, 3, 0)
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha Daga, I couldn't get that point. How it is happening? Since second enhanced for loop works correctly (traversed through according to index position), so why is that for first?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok , I hope you know how for each loop works it iterates through elements in the loop.

when first iteration occurs it sets 'i' to the first value in array that is 1. Now the next statement sets the element at index represented by 'i' to 0 so the element at index 1 is set to 0. Now when the second iteration occurs 'i' is set to the second element that is the element at index 1 which is 0, so now the next statement will set the element at index 0 will be changed to 0. the third iteration sets 'i' to 3 value at the index 2 and hence the value at index 3 will be changed to 0. and in the last iteration the i will be set to 0 beacuase now index 3 has been set to 0 in the last iteration and hence the next statement will change the first value to 0.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This indeed is a good question and the explanation ever better.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea.. Me too.


This gives me RTE(ArrayIndexOutOfBoundsException), At line 8. Please help!
 
author
Posts: 23923
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

Abimaran Kugathasan wrote:
This gives me RTE(ArrayIndexOutOfBoundsException), At line 8. Please help!



If you were able to follow the explanation for the first example, you should be able to figure out this example -- it is basically doing the same thing with different numbers.

Henry
 
Sheriff
Posts: 9695
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Abimaran What will happen when the first for loop will iterate for the third time?? At that time i will be 8, so the statement inside the loop will be: x[8] = 0; and that will result in an ArrayIndexOutOfBoundsException...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh! I confused.... Shit! That i refers the elements of that array, not the index. Soooo Sorry. What a stupid mistake! I start to forget all.......
Sorry heba mahmoud.
Sorry Neha Daga.
Sorry KrishnaPrasad raghavan.

I may confused All.

Thanks a lot Ankit Garg! Thanks.........
 
reply
    Bookmark Topic Watch Topic
  • New Topic