gpt4 book ai didi

java - 获取Java嵌套ArrayList中的元素个数

转载 作者:行者123 更新时间:2023-11-29 04:20:07 24 4
gpt4 key购买 nike

我正在尝试扫描 Android wifi 网络并将它们存储在数组列表中。我的目标如下。

1.扫描wifi网络并将它们存储在一个ScanObject的ArrayList中

private List<ScanResult> results; // A list of ScanResult is given by system
private ArrayList<ScanObject> scanObjectList = new ArrayList<ScanObject>;

for(int i=0;i<results.size();i++){
String mac = results.get(i).BSSID;
int rssi = results.get(i).level;
scanObject = new ScanObject(mac,rssi);
scanObjectList.add(i,scanObject);
}

2.将ScanObject的ArrayList与系统时间一起存储到ScanInfo对象中

    long systemTime = System.currentTimeMillis();
scanInfo = new ScanInfo(systemTime,scanObjectList);

3.将ScanInfo对象添加到ScanInfo对象的ArrayList中

private ArrayList<ScanInfo> listOfScanInfoObjects = new ArrayList<ScanInfo>();
listOfScanInfoObjects.add(scanInfo);

当我尝试显示 listOfScanInfoObjects 中的对象时,我只能显示我只看到最后一次扫描。例如,如果我执行以下操作,经过几次扫描后,我只会看到最后一个对象中的元素数。所有其他人都给我零值(value)。我想了解我在这里做错了什么。

for(int q=0;q<listOfScanInfoObjects.size();q++) {
System.out.println("SIZE: "+listOfScanInfoObjects.get(q).getScanObjects().size());
}

我的ScanObject类如下;

public class ScanObject {
private String BSSID; // MAC address of the AP
private int RSSI; // RSSI value in dBm

public ScanObject(String mac, int rssi) {
setBSSID(mac);
setRSSI(rssi);
}
// setter methods
public void setBSSID(String mac){
this.BSSID = mac;
}
public void setRSSI(int rssi){
this.RSSI = rssi;
}
// getter methods
public String getBSSID(){
return this.BSSID;
}
public int getRSSI(){
return this.RSSI;
}
}

我的ScanInfo类如下;

public class ScanInfo {
private long timeStamp;
private ArrayList<ScanObject> scanObjectList;

public ScanInfo(long time, ArrayList<ScanObject> scanList) {
setTimeSamp(time);
setScanObjects(scanList);
}
// setter methods
public void setTimeSamp(long time){
this.timeStamp = time;
}
public void setScanObjects(ArrayList<ScanObject> scanList){
scanObjectList = new ArrayList<ScanObject>();
this.scanObjectList = scanList;
}

// getter methods
public long getTimeStamp(){
return this.timeStamp;
}
public ArrayList<ScanObject> getScanObjects(){
return this.scanObjectList;
}
}

整体代码如下。

public void main(){
processRawResult();
listOfScanInfoObjects.add(index,scanInfo);
for(int q=0;q<listOfScanInfoObjects.size();q++) {
System.out.println("SIZE:"+listOfScanInfoObjects.get(q).getScanObjects().size());
}

scanObjectList.clear();
index++;
}

// process results object
public void processRawResult(){
long systemTime = System.currentTimeMillis();
scanObjectList = new ArrayList<ScanObject>();
for(int i=0;i<results.size();i++){
String mac = results.get(i).BSSID;
int rssi = results.get(i).level;
scanObject = new ScanObject(mac,rssi);
scanObjectList.add(i,scanObject);
}
scanInfo = new ScanInfo(systemTime,scanObjectList);
}

提前致谢。

最佳答案

@哈萨拉

 public void setScanObjects(ArrayList<ScanObject> scanList){
scanObjectList = new ArrayList<ScanObject>();
this.scanObjectList = scanList;
}

此处您引用的是已通过的列表。因此,当您清除列表时,scanObjectList 中的所有数据都会清除 ScanInfo 列表中包含的数据,因为两者相同。

public void main(){
final ScanInfo scanInfo = processRawResult();
listOfScanInfoObjects.add(scanInfo);
for(int q=0;q<listOfScanInfoObjects.size();q++) {
System.out.println("SIZE:"+ listOfScanInfoObjects.get(q).getScanObjects().size());
}

scanObjectList.clear();
index++;
}

所以在 setScanObjects(ArrayList scanList) 方法中使用 this.scanObjectList.addAll(scanList); 而不是 this.scanObjectList = scanList;

关于java - 获取Java嵌套ArrayList中的元素个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com