java引用数组中的对象

ali*_*irv 2 c++ java arrays algorithm indexing

是否有一个类似的函数indexof()将搜索字符串数组(最好是未排序)的字符串并返回它的索引?(或者可能是纵坐标值?)

例如我正在尝试:

String[] colours= {"Red", "Orange", "Yellow"};

System.out.println("indexOf(Red) = " +
        colours.indexOf("Red"));
Run Code Online (Sandbox Code Playgroud)

......但没有成功.

谢谢.

AV

ps这最终需要在二维数组中工作(如果重要的话)

Jig*_*shi 5

String[] colours= {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue"};

Arrays.asList(colours).indexOf("Red"); // 0
Run Code Online (Sandbox Code Playgroud)

或者,
如果订单无关紧要

Arrays.sort(colours);   
Arrays.binarySearch(colours,"Red");//binary search takes sorted[natural order] array as input
Run Code Online (Sandbox Code Playgroud)