搜索arrayLists

时间:2014-12-13 21:29:30

标签: java search arraylist indexing

您好我正在创建一个存储trackNum,名称和持续时间的简单mp3数据库。我需要搜索数组列表并获取搜索索引。

这就是我现在所拥有的。

//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. 如何搜索arrayList
  2. 我需要能够获取搜索索引以添加曲目名称及其持续时间。

1 个答案:

答案 0 :(得分:2)

我建议使用

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

如果名称已排序,则可以使用binarySearch。