如何在字符串数组中找到给定字符串的索引?
How to find an index of given String in array of strings?
当然,解决方案如下:
public long myFunc(String name) throws Exception {
for(int i=0;i<amount;i++){
if(this.otherString[i].equals(name))
return longArray[i];
}
throw new Exception("Not found");
}
然而,似乎并不是这样。
你可以使用 Guava,那么你的代码可以是这样的
String[] stringArray = {"s1", "s2", "s3"};
int index = Iterators.indexOf(Iterators.forArray(stringArray), new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.equals("s2");
}
});
或更简单
int index = Arrays.asList(stringArray).indexOf("s2");
您的代码也可以像这样
public class Finder {
private String[] stringArray = {"s1", "s2", "s3"};
public int findIndex(String name) {
for (int i = 0; i < stringArray.length; i++) {
if (stringArray[i].equals(name))
return i;
}
throw new RuntimeException("Not found");
}
public static void main(String... s) {
int index = new Finder().findIndex("s1");
System.out.println(index);
}
}
这可能是完全错误的,但问题不只是在 if 语句中缺少大括号吗?我是 java 语言的新手,这个例子可能很混乱,但它使用与你的问题相同的结构并且工作得很好:
public class random_class {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] a = new String[] {"hi!", "hello world!", "ho ho ho world!"};
int b = getHWIndex(a);
System.out.println(b);
}
public static int getHWIndex(String[] stringArray){
int i;
Boolean test = false;
for(i=0;i<stringArray.length;i++){
if(stringArray[i].equals("hello world!")){
test = true;
break;
}
}
if(test == true){
return i;}else{
return i = 0; // this is not a good answer...
//but as you return an int I could not think on a quick way to fix the return when there is no match.
}
}
}
您可以通过调试器 运行 您的代码并找出它不起作用的原因,或者向您的原始代码添加一些 println 跟踪,您将看到问题所在:
public long myFunc(String name) throws Exception {
System.out.println("Looking for: " + name);
for (int i = 0; i < amount; i++){
if(this.otherString[i].equals(name))
return longArray[i];
System.out.printf("%4d: \"%s\": No match.%n", i, this.otherString[i]);
}
for (int i = amount; i < this.otherString.length; i++)
System.out.printf("%4d: \"%s\": Not checked.%n", i, this.otherString[i]);
throw new Exception("Not found");
}
顺便说一下,请确保您正确解释了该方法的行为。可能是它找到它但抛出 ArrayIndexOutOfBoundsException 因为 i
大于 longArray.length,而您误解为您明确抛出的异常?
原来“\u0000”位于本应相等的字符串末尾。这不会显示在打印中。下次在调试的检查上狠点。感谢您提出的所有建议,很抱歉浪费了您的时间。
当然,解决方案如下:
public long myFunc(String name) throws Exception {
for(int i=0;i<amount;i++){
if(this.otherString[i].equals(name))
return longArray[i];
}
throw new Exception("Not found");
}
然而,似乎并不是这样。
你可以使用 Guava,那么你的代码可以是这样的
String[] stringArray = {"s1", "s2", "s3"};
int index = Iterators.indexOf(Iterators.forArray(stringArray), new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.equals("s2");
}
});
或更简单
int index = Arrays.asList(stringArray).indexOf("s2");
您的代码也可以像这样
public class Finder {
private String[] stringArray = {"s1", "s2", "s3"};
public int findIndex(String name) {
for (int i = 0; i < stringArray.length; i++) {
if (stringArray[i].equals(name))
return i;
}
throw new RuntimeException("Not found");
}
public static void main(String... s) {
int index = new Finder().findIndex("s1");
System.out.println(index);
}
}
这可能是完全错误的,但问题不只是在 if 语句中缺少大括号吗?我是 java 语言的新手,这个例子可能很混乱,但它使用与你的问题相同的结构并且工作得很好:
public class random_class {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] a = new String[] {"hi!", "hello world!", "ho ho ho world!"};
int b = getHWIndex(a);
System.out.println(b);
}
public static int getHWIndex(String[] stringArray){
int i;
Boolean test = false;
for(i=0;i<stringArray.length;i++){
if(stringArray[i].equals("hello world!")){
test = true;
break;
}
}
if(test == true){
return i;}else{
return i = 0; // this is not a good answer...
//but as you return an int I could not think on a quick way to fix the return when there is no match.
}
}
}
您可以通过调试器 运行 您的代码并找出它不起作用的原因,或者向您的原始代码添加一些 println 跟踪,您将看到问题所在:
public long myFunc(String name) throws Exception {
System.out.println("Looking for: " + name);
for (int i = 0; i < amount; i++){
if(this.otherString[i].equals(name))
return longArray[i];
System.out.printf("%4d: \"%s\": No match.%n", i, this.otherString[i]);
}
for (int i = amount; i < this.otherString.length; i++)
System.out.printf("%4d: \"%s\": Not checked.%n", i, this.otherString[i]);
throw new Exception("Not found");
}
顺便说一下,请确保您正确解释了该方法的行为。可能是它找到它但抛出 ArrayIndexOutOfBoundsException 因为 i
大于 longArray.length,而您误解为您明确抛出的异常?
原来“\u0000”位于本应相等的字符串末尾。这不会显示在打印中。下次在调试的检查上狠点。感谢您提出的所有建议,很抱歉浪费了您的时间。