• 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

Inquisition mock doubt

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was a mock question I came across...



while trying to compile this code..I expected the element at position 1 in the ArrayList to be removed and the output would be 2...I checked the ArrayList doc and it says "The method removes the element at the specified position in this list."...so how come the output is 1...

I tried adding more elements and went on removing from position 1 and the code is behaving strangely...

I compiled this...



and the output is 1 4 ....please help me out here..
[ December 20, 2007: Message edited by: sugantha Jeevankumar ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sugantha

array list index always starts with 0
so
at index 0 value is 1
at index 1 value is 2
so if you are deleting 1 the it will remove the value at index 1 means 2 so answer will be 1

in second case
at index 0 it is 1
at index 1 it is 2
at index 2 it is 3
at index 3 it is 4
if remove(1) it will delete value 2

now
at index 0 it is 1
at index 1 it is 3
at index 2 it is 4
so second remove(1) will remove value 3
so answer will be 1 and 4

which is right
 
Sugantha Jeevankumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Amit...That was stupid of me to not notice.Thanks for the clear explanation...
 
Amit Kathpal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is okay dear
reply
    Bookmark Topic Watch Topic
  • New Topic