This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Using indexOf(Object o) for String[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using indexOf(Object o) for String[]" Watch "Using indexOf(Object o) for String[]" New topic
Author

Using indexOf(Object o) for String[]

Eric Crockett
Greenhorn

Joined: Jan 24, 2005
Posts: 25
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

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jan 17, 2006
Posts: 1296
Also for a sorted array you can use


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using indexOf(Object o) for String[]