我有这个问题:
我写这个函数是因为我需要获取字符串数组中特定字符串 st
出现的索引
static public int indicestring(String[] array, String st) {
int ret = -1;
for (int i = 0; i < array.length; i++){
if (st.equals(array[i])) {
ret=i;
break;
}
}
return ret;
}
然后我调用:
System.out.println("indicestring(NODO,"ET2"));
我得到了正确的号码。
但是当我这样做的时候:
String[] arcos2 = linea.split("-");//reading from a file and separating by "-"
String aux = arcos2[1];
System.out.println(arcos2[1]);
System.out.println(aux);
if (aux.equals(arcos2[1])) {
System.out.println("Is equal 1");
}
if (aux.equals("ET2")) {
System.out.println("Is equal 2");
}
if ("ET2".equals(aux)) {
System.out.println("is equal 3");
}
前两次打印是 ET2
,但随后只打印了 3 个 ifs 是“等于 1”......问题是我有将近 200 个节点,如“ET2”,而且只有3 失败并在第一个函数中给了我 -1 ...
我的问题是....我是否使用了错误的数组来保存和比较数据,因为如果 aux=arcos2[1]="ET2",为什么 'aux.equals("ET2") ' 或 ' arcos2[1].equals("ET2)' 不工作
?您可以推荐尝试使用另一个功能吗?(我尝试使用 compareTo() == 0
更改 equals,但这也不起作用,还建议进行修整)。
之前,我在比较两个数组时遇到了类似的错误:
if(a[0] == b[0] && a[1] == b[1])
有一个案例明明是正确的,却被忽略了…… 但是当我将其更改为:
if (Arrays.equals(a, b))
有没有这样的变化
最佳答案
您应该在代码中放置调试断点并添加表达式监视以确定问题的根本原因。
关于Java:我比较了两个字符串,但它没有识别出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085367/