Java ArrayList Contains() 方法无效
Java ArrayList Contains() method not working
我毫不怀疑 contains() 方法工作正常,但显然我做错了什么。我正在开发一个 android 应用程序,我正在检测 WiFi 并将 WiFi 列在列表视图中。我试图过滤掉重复出现的同一接入点,因此我创建了一个数组来跟踪重复项。这是我的代码:
public void getWifi(){
ListView wifiList = (ListView)findViewById(R.id.listView);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> apList = wifiManager.getScanResults();
wifiManager.startScan();
List<String> wifees = new ArrayList<>();
//split info into arrays
for(int i=0; i < apList.size(); i++){
String[] daList = String.valueOf(apList.get(i)).split(",");
String info = "";
List<String> SSIDs = new ArrayList<>();
for (String listItem : daList) {
String topic = String.valueOf(listItem.split(":")[0]);
String val = listItem.split(":")[1];
Log.d(TAG, topic);
if (Objects.equals(topic, "SSID")) {
if(SSIDs.contains(val)){
Log.d(TAG, "CONTAINS");
break;
} else {
SSIDs.add(val);
info = val;
}
} else if (Objects.equals(topic, " level")){
String strength = "";
if (Integer.parseInt(val.substring(1)) >= -35) {
strength = "100%";
} else if (Integer.parseInt(val.substring(1)) <= -35 && Integer.parseInt(val.substring(1)) >= -65) {
strength = "50%";
} else {
strength = "1%";
}
info += " " + strength;
}
}
wifees.add(info);
}
assert wifiList != null;
ArrayAdapter apter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, wifees);
wifiList.setAdapter(apter);
}
这是 apList 项目的内容,或者我调用 apList.get(i) 时得到的内容:
SSID: SCHOOL-GUEST, BSSID: 04:04:04:04:04:04, capabilities: [ESS], level: -91, frequency: 2412, timestamp: 72500687089, distance: ?(cm), distanceSd: ?(cm), passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, 80211mcResponder: is not supported
这是该代码的输出(大致是因为它在 phone 上,出于安全原因我不想包含 AP 名称):
SCHOOL-WIRELESS 100%
SCHOOL-SECURE 50%
SCHOOL-GUEST 50%
SCHOOL WIRELESS 1%
SCHOOL_WIRELESS 1%
SCHOOL-SECURE 1%
SCHOOL-GUEST 1%
bxz1872 50%
...
...
...
(so on and so forth)
如果我能得到第二个意见就好了。我觉得我错过了一些明显的东西。谢谢!
.contains
不起作用,因为您在 for(int i=0; i < apList.size(); i++){
中调用了 List<String> SSIDs = new ArrayList<>();
。您基本上为每个循环迭代创建新的空列表。您应该在外部 for 循环之前调用它一次:
List<String> SSIDs = new ArrayList<>();
for(int i=0; i < apList.size(); i++){
...
}
我毫不怀疑 contains() 方法工作正常,但显然我做错了什么。我正在开发一个 android 应用程序,我正在检测 WiFi 并将 WiFi 列在列表视图中。我试图过滤掉重复出现的同一接入点,因此我创建了一个数组来跟踪重复项。这是我的代码:
public void getWifi(){
ListView wifiList = (ListView)findViewById(R.id.listView);
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> apList = wifiManager.getScanResults();
wifiManager.startScan();
List<String> wifees = new ArrayList<>();
//split info into arrays
for(int i=0; i < apList.size(); i++){
String[] daList = String.valueOf(apList.get(i)).split(",");
String info = "";
List<String> SSIDs = new ArrayList<>();
for (String listItem : daList) {
String topic = String.valueOf(listItem.split(":")[0]);
String val = listItem.split(":")[1];
Log.d(TAG, topic);
if (Objects.equals(topic, "SSID")) {
if(SSIDs.contains(val)){
Log.d(TAG, "CONTAINS");
break;
} else {
SSIDs.add(val);
info = val;
}
} else if (Objects.equals(topic, " level")){
String strength = "";
if (Integer.parseInt(val.substring(1)) >= -35) {
strength = "100%";
} else if (Integer.parseInt(val.substring(1)) <= -35 && Integer.parseInt(val.substring(1)) >= -65) {
strength = "50%";
} else {
strength = "1%";
}
info += " " + strength;
}
}
wifees.add(info);
}
assert wifiList != null;
ArrayAdapter apter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, wifees);
wifiList.setAdapter(apter);
}
这是 apList 项目的内容,或者我调用 apList.get(i) 时得到的内容:
SSID: SCHOOL-GUEST, BSSID: 04:04:04:04:04:04, capabilities: [ESS], level: -91, frequency: 2412, timestamp: 72500687089, distance: ?(cm), distanceSd: ?(cm), passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, 80211mcResponder: is not supported
这是该代码的输出(大致是因为它在 phone 上,出于安全原因我不想包含 AP 名称):
SCHOOL-WIRELESS 100%
SCHOOL-SECURE 50%
SCHOOL-GUEST 50%
SCHOOL WIRELESS 1%
SCHOOL_WIRELESS 1%
SCHOOL-SECURE 1%
SCHOOL-GUEST 1%
bxz1872 50%
...
...
...
(so on and so forth)
如果我能得到第二个意见就好了。我觉得我错过了一些明显的东西。谢谢!
.contains
不起作用,因为您在 for(int i=0; i < apList.size(); i++){
中调用了 List<String> SSIDs = new ArrayList<>();
。您基本上为每个循环迭代创建新的空列表。您应该在外部 for 循环之前调用它一次:
List<String> SSIDs = new ArrayList<>();
for(int i=0; i < apList.size(); i++){
...
}