This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes index of an element in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "index of an element in an array" Watch "index of an element in an array" New topic
Author

index of an element in an array

krishna prasad gunasekaran
Ranch Hand

Joined: Jul 25, 2006
Posts: 158
good day,
hw to find the index of an element in an array.


have a great day,
krishna prasad
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

java.util.List has an indexOf method (providing your equals method is implemented correctly), and can convert from an Array to a List using java.util.Arrays.asList
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20173
    
  47

Well, if the elements of the array are sorted, then you could use the java.util.Arrays class. Otherwise, I believe you have to loop through the array to find the element that you want.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: index of an element in an array
 
It's not a secret anymore!