Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lastIndexOf() Question

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use the ArrayUtils class from http://jakarta.apache.org/commons/lang/
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, stupid of me. Since arrays are Objects of course they have these...
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is lastIndexOf() in the ArrayList class, maybe?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In all Lists.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic