• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the last element in ArrayList

 
indu yeturu
Ranch Hand
Posts: 123
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi am trying to get the elements in array list by passing the indices.


If "to" is equal to the size of the list.,then only the exception is coming ..


I checked the Index as list.indexOf(det), then it shows the last inde Index as 13 and size of the list as 14..
Please give me solution..

thanks
indu
indu


 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lists (like Java arrays) are zero based indexing... i.e the first element's index is zero
 
adil qureshi
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
size of the list will be 14 only but indexing will be like 0 to 13 ,just handle this .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP. For more information, please read this.

This post has been moved to a more appropriate forum.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are methods in the List interface which provide the size of the List; it is easy to use that size - 1 to access the last element.
You can get nasty out-by-one errors if you use <= instead of < (or vice versa) in a for loop.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic