Use the string.equals(Object other)
function to compare strings, not the ==
operator.
(使用string.equals(Object other)
函数来比较字符串,而不是==
运算符。)
The function checks the actual contents of the string, the ==
operator checks whether the references to the objects are equal.
(该函数检查字符串的实际内容, ==
运算符检查对象的引用是否相等。)
Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==
, but it's better not to rely on that. (请注意,字符串常量通常是“实例化”,这样两个具有相同值的常量实际上可以与==
进行比较,但最好不要依赖它。)
if (usuario.equals(datos[0])) {
...
}
NB: the compare is done on 'usuario' because that's guaranteed non-null in your code, although you should still check that you've actually got some tokens in the datos
array otherwise you'll get an array-out-of-bounds exception.
(注意:比较是在'usuario'上完成的,因为你的代码中保证非null,尽管你仍然应该检查你在datos
阵列中确实得到了一些标记,否则你会得到一个数组超出范围例外。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…