File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes lastIndexOf()  Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "lastIndexOf()  Question" Watch "lastIndexOf()  Question" New topic

lastIndexOf() Question

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
If i have an array of integers called, say, integerArray[], and i am looking for the last location that contains, say, 1, then how do i use the lastIndexOf() method, as it does not seem to work when i try it.

my code is similar to:

[ November 13, 2005: Message edited by: Sam Bluesman ]

Moosey knows best
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

Arrays don't have methods and have only 1 field, length.
java.util.Arrays has some utility methods for arrays, but no lastIndexOf, so you'll have to write your own utility class that implements it. Just traverse the array backwards, starting at intArray.length - 1.

How To Ask Questions How To Answer Questions
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Or use the ArrayUtils class from

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Originally posted by Rob Spoor:
Arrays don't have methods

Actually they have all the methods of the class java.lang.Object: equals(), hashCode(), wait(), notify(), notifyAll(), toString(), finalize(), getClass(), and clone() (which is public in all array classes.)

[Jess in Action][AskingGoodQuestions]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

Right, stupid of me. Since arrays are Objects of course they have these...
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45338
Is lastIndexOf() in the ArrayList class, maybe?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20193

In all Lists.
I agree. Here's the link:
subject: lastIndexOf() Question
It's not a secret anymore!