IeuanW
IeuanW

Reputation: 298

Searching arrayLists

Hi im creating a simple mp3 database that stores a trackNum, name and duration. I need to search an array list and get the index of the search.

Here is what i have at the moment.

//My methods
public void searchTrackNum(Intager trackNumber){
    System.out.println(trackNum + ": " + name[index] + " " + duration[index]);
}
public void searchName(String name){
    System.out.println(trackNum + ": " + name[index] + " " + duration[index]);
}

//Using the methods
case 4:
    System.out.println("What name would you like to search for: 1-Track Number or 2-Name");
    int question = in.nextInt();
        if(question == 1){
            System.out.println("Please enter the track Number?");
            meth.searchTrackNum(in.nextInt());
        }
        if(question == 2){
            System.out.println("Please enter the Name?");
            meth.searchName(in.next());
        }
        else{
            System.out.println("Pease enter 1  or 2.");
        }
    break;
  1. How do i search an arrayList
  2. I need to be able to get the index of the search to add the name of the track and the duration of it.

Upvotes: 1

Views: 53

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

I suggest using

int index = Arrays.asList(names).indexOf(nameToLookFor);

If the names are sorted, you can use binarySearch instead.

Upvotes: 2

Related Questions