2 回答
TA贡献1836条经验 获得超5个赞
public void search() {
String[] arrayString = new String[]{
"Administrator",
"Vale.Vicky",
"Lane.Lois",
"Kent.Clark",
"Wayne.Bruce",
"Parker.Peter",
"Rogers.Steve",
"Luther.Lex",
"Osborn.Harry",
"Tony.Stark"};
String searchString = "Rogers.Steve";
Arrays.sort(arrayString);
System.out.println(Arrays.binarySearch(arrayString, searchString));
}
这其中的关键部分是
Arrays.binarySearch(arrayString, searchString)
TA贡献1878条经验 获得超4个赞
我想你正在寻找这样的东西
String[] arrayString=new String[]{
"Administrator",
"Vale.Vicky",
"Lane.Lois",
"Kent.Clark",
"Wayne.Bruce",
"Parker.Peter",
"Rogers.Steve",
"Luther.Lex",
"Osborn.Harry",
"Tony.Stark"};
String searchString="Rogers.Steve";
int position = 0;
for (int i = 0; i < arrayString.length; i++) {
if(arrayString[i] == searchString ){
position = i;
}
}
if( position > 0 ){
System.out.println("Position: " + position);
} else {
System.out.println(searchString + " not found" );
}
添加回答
举报