This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
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 Building Microservices this week in the Design 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: 20083

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: 24190

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: 20083

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

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

Joined: Oct 27, 2005
Posts: 20083

In all Lists.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: lastIndexOf() Question
It's not a secret anymore!