Android 使用Arrays.asList和compare
我有一个如下所示的数组:Android 使用Arrays.asList和compare,android,arrays,string,Android,Arrays,String,我有一个如下所示的数组: this.ntpServers[0][0] = "Global"; this.ntpServers[0][1] = "pool.ntp.org"; this.ntpServers[1][0] = "Africa"; this.ntpServers[1][1] = "africa.pool.ntp.org"; this.ntpServers[2][0] = "Asia"; this.ntpServers[2][1] = "asia.pool.ntp.org"; this.
this.ntpServers[0][0] = "Global";
this.ntpServers[0][1] = "pool.ntp.org";
this.ntpServers[1][0] = "Africa";
this.ntpServers[1][1] = "africa.pool.ntp.org";
this.ntpServers[2][0] = "Asia";
this.ntpServers[2][1] = "asia.pool.ntp.org";
this.ntpServers[3][0] = "Europe";
this.ntpServers[3][1] = "europe.pool.ntp.org";
this.ntpServers[4][0] = "North America";
...
this.ntpServers[85][0] = ...
this.ntpServers[85][1] = ...
我在另一个字符串中有一个国家,我试图使用下一个代码来比较列表中是否存在,但Iit不会返回true,因为它必须是true。如果我查“亚洲”,那是真的。但有点不对劲
gettedCountry是一个字符串
public int existNTP(String[][] list) {
if(Arrays.asList(list).contains(gettedCountry)){
Log.i("Found", "Found");
}
return position;
}
谢谢您的帮助。数组。asList(list)
将返回String[]
的ArrayList
。如果您需要检查第一项:
ArrayList<String[]> arr=Arrays.asList(list);
for(String[] arry : arr){
if(arry[0].equals(gettedCountry)) /* Do your stuff */;
}
ArrayList arr=Arrays.asList(列表);
for(字符串[]arry:arr){
如果(arry[0].equals(gettedCountry))/*做你的事*/;
}
制作适当的对象,将二维数组转换为列表(推荐)
或
在数组上迭代:
int position = 0;
for (String[] entry : ntpServers) {
if (entry[0].equals(country)) return position;
++position;
}
return -1; // Not found is an invalid position like -1
但是文档说,如果包含该对象,它将返回true。是的,但是您正在搜索的对象是字符串数组对象而不是字符串,因此您要进行如下检查(String[]==String)
ArrayList arr=Arrays.asList(list)代码>是不需要的。你可以直接迭代arr
是的,我做了,但我在这里读到一篇帖子,上面说Arrays.asList进行比较比这种方法更快、更有效。我很想得到这个参考。看起来很奇怪