Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting the last element in ArrayList  RSS feed

 
Ranch Hand
Posts: 123
  • 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


 
Ranch Hand
Posts: 580
Eclipse IDE
  • 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
 
Ranch Hand
Posts: 48
  • 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 .
 
Author and ninkuma
Marshal
Posts: 66535
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
Marshal
Posts: 58345
178
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!