File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "lastIndexOf()  Question" Watch "lastIndexOf()  Question" New topic
Author

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:



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

Moosey knows best
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Or use the ArrayUtils class from http://jakarta.apache.org/commons/lang/


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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

In all Lists.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: lastIndexOf() Question
 
Similar Threads
StringBuffer question
removing last occurence from string
Mughal String.lastIndexOf
collection question from nikos...
MYFaces JSF UploadedFile Object