wood burning stoves
The moose likes Beginning Java and the fly likes Getting the last element in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting the last element in ArrayList" Watch "Getting the last element in ArrayList" New topic

Getting the last element in ArrayList

indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
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..


James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Lists (like Java arrays) are zero based indexing... i.e the first element's index is zero

Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
adil qureshi
Ranch Hand

Joined: Jul 11, 2008
Posts: 48
size of the list will be 14 only but indexing will be like 0 to 13 ,just handle this .

SCJP 1.5,SCWCD 1.5
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63867

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.

[Asking smart questions] [About Bear] [Books by Bear]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46410
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Getting the last element in ArrayList
It's not a secret anymore!