是否可以搜索数组并返回值的索引

use*_*160 1 java arrays search

我正在编写一个简单的Java程序,它基本上存储了之前已经在图表中的一系列艺术家; 这是我目前为程序编写的代码

package searching;

import java.util.*;

public class Searching {
    public static void main(String[] args) {          
        Scanner scanner = new Scanner(System.in);

        String artists[] = {"Rihanna", "Cheryl Cole", "Alexis Jordan", "Katy Perry", "Bruno Mars",
                        "Cee Lo Green", "Mike Posner", "Nelly", "Duck Sauce", "The Saturdays"};

        System.out.println("Please enter an artist...");
        String artist = scanner.nextLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是想知道,用户是否可以输入其中一位艺术家的名字,获取搜索数组的代码并返回该值的索引?如果是这样,我怎么会这样做,因为我不知道从哪里开始...提前感谢!

And*_*mas 5

对于未排序的数组,一个选项是将艺术家放在List中并使用List.indexOf().

 List<String> artistsList = Arrays.asList( artists );
 ...
 int index = artistsList.indexOf( artist );
Run Code Online (Sandbox Code Playgroud)

如果艺术家已经排序,您可以使用Arrays.binarySearch().