Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using indexOf(Object o) for String[]

 
Eric Crockett
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose one thing that might help me understand this is to know what classes are inherited when a String array (or any kind of array excluding ArrayLists) is created. I am positive that I have used some method (I don't remember it being the indexOf method) to find the index of an object within an array.

For some reason, when I try



I get an error saying "cannot find method indexOf"

If arrays inherit class Arrays, it HAS a method
public int indexOf(Object o)
since a string is an object.....
(It also says the same thing if I cast it to an Object)

As I said, I remember doing this with another method......I just can't remember what that was...
If I can get indexOf to work, that will be fine.

Thanks in advance!
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every array is a direct subclass of Object. They inherit all the methods of Object and additionally have a static field "length". There is no method "indexOf(Object)" in Arrays. You seem to be thinking of a List, which is not at all the same thing as an array. There is a class called ArrayList which is an implementation of List that uses an array internally for a backing, but that does not in any way shape or form make it an array. You could accomplish this by converting it to a List:

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also for a sorted array you can use
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic