获取Java嵌套ArrayList中的元素数量

时间:2018-04-20 05:17:43

标签: java android arraylist

我正在尝试扫描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.使用系统时间在ScanInfo对象中存储ScanObject的ArrayList

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

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

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

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

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);
}

提前致谢。

3 个答案:

答案 0 :(得分:1)

@Hasala

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

这里你引用传递列表。因此,当您清除scanObject列表中的所有数据时,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++;
}

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

答案 1 :(得分:1)

问题出在以下代码中:

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);
}

在ArrayList中添加Object时,您将添加其引用而不仅仅是其值。对对象的任何更改都将导致ArrayList中可用的对象发生更改。

在您的情况下,您总是重新分配scanObjectList&amp;的价值。 scanInfo两行scanInfo = new ScanInfo(systemTime,scanObjectList);&amp; scanObjectList = new ArrayList<ScanObject>();

请尝试以下代码:

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++;
}

// process results object
public ScanInfo processRawResult(){
    long systemTime = System.currentTimeMillis();
    final 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);
    }
    return new ScanInfo(systemTime,scanObjectList);
}

答案 2 :(得分:1)

这是我的建议和假设。 我假设我有一个类ScanResultStore,其中包含一些外部信息,如下所示:

class ScanResultStore {
    private List<ScanResult> results; // A list of ScanResult is given by
                                        // system
    private ArrayList<ScanObject> scanObjectList = new ArrayList<ScanObject>();
    private ArrayList<ScanInfo> listOfScanInfoObjects = new ArrayList<ScanInfo>();
    private int index = 0;
    ScanInfo scanInfo = null;

    public void setScanResult(List<ScanResult> results)
    {
        this.results = results;
    }

    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();
    }

    // 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 scanObject = new ScanObject(mac, rssi);
            scanObjectList.add(i, scanObject);
        }

        scanInfo = new ScanInfo(systemTime, scanObjectList);
    }
}

我修改了你的类并为外部信息添加了一个方法setScanResult。

ScanResult类如下所示,

class ScanResult {
    public String BSSID; // MAC address of the AP
    public int RSSI; // RSSI value in dBm
    public int level;
}

然后,我们可以有类似的入口点,输出将是

SIZE:3 SIZE:1

public static void main(String[] args)
{
    ScanResultStore clsStore = new ScanResultStore();
    List<ScanResult> results = new ArrayList<ScanResult>();

    ScanResult result1 = new ScanResult();
    result1.BSSID="AF-DF-CF-ED-EF";
    result1.RSSI=2;
    result1.level=4;
    results.add(result1);
    ScanResult result2 = new ScanResult();
    result2.BSSID="AD-DF-CF-ED-EF";
    result2.RSSI=1;
    result2.level=3;
    results.add(result2);
    ScanResult result3 = new ScanResult();
    result3.BSSID="AD-DG-CF-ED-EF";
    result3.RSSI=2;
    result3.level=5;
    results.add(result3);

    clsStore.setScanResult(results);

    clsStore.main();

    clsStore = new ScanResultStore();
    results = new ArrayList<ScanResult>();

    ScanResult result4 = new ScanResult();
    result3.BSSID="AD-DG-CF-ED-TF";
    result3.RSSI=1;
    result3.level=5;
    results.add(result4);

    clsStore.setScanResult(results);

    clsStore.main();
}

很明显你有一个ScanResultStore引用,没关系。 您在类中有3个扫描对象,但在下面的代码后您将只有一个扫描对象。

clsStore = new ScanResultStore(); results = new ArrayList();

ScanResult result4 = new ScanResult();
result3.BSSID="AD-DG-CF-ED-TF";
result3.RSSI=1;
result3.level=5;
results.add(result4);

clsStore.setScanResult(results);

clsStore.main();

我认为您可以仔细查看我在此处描述的案例。

这是另一种情况,但输出相同。

public static void main(String[] args)
{
    ScanResultStore clsStores[] = new ScanResultStore[2];

    List<ScanResult> results = new ArrayList<ScanResult>();

    ScanResult result1 = new ScanResult();
    result1.BSSID="AF-DF-CF-ED-EF";
    result1.RSSI=2;
    result1.level=4;
    results.add(result1);
    ScanResult result2 = new ScanResult();
    result2.BSSID="AD-DF-CF-ED-EF";
    result2.RSSI=1;
    result2.level=3;
    results.add(result2);
    ScanResult result3 = new ScanResult();
    result3.BSSID="AD-DG-CF-ED-EF";
    result3.RSSI=2;
    result3.level=5;
    results.add(result3);

    clsStores[0] = new ScanResultStore();

    clsStores[0].setScanResult(results);

    clsStores[0].main();

    clsStores[1] = new ScanResultStore();
    clsStores[1] = new ScanResultStore();
    results = new ArrayList<ScanResult>();

    ScanResult result4 = new ScanResult();
    result3.BSSID="AD-DG-CF-ED-TF";
    result3.RSSI=1;
    result3.level=5;
    results.add(result4);

    clsStores[1].setScanResult(results);

    clsStores[1].main();
}

完整来源:

package com.tobee.tests.oop;

import java.util.ArrayList;
import java.util.List;

public class StaticNoStaticTest {

    public static void main(String[] args)
    {
        ScanResultStore clsStores[] = new ScanResultStore[2];

        List<ScanResult> results = new ArrayList<ScanResult>();

        ScanResult result1 = new ScanResult();
        result1.BSSID="AF-DF-CF-ED-EF";
        result1.RSSI=2;
        result1.level=4;
        results.add(result1);
        ScanResult result2 = new ScanResult();
        result2.BSSID="AD-DF-CF-ED-EF";
        result2.RSSI=1;
        result2.level=3;
        results.add(result2);
        ScanResult result3 = new ScanResult();
        result3.BSSID="AD-DG-CF-ED-EF";
        result3.RSSI=2;
        result3.level=5;
        results.add(result3);

        clsStores[0] = new ScanResultStore();

        clsStores[0].setScanResult(results);

        clsStores[0].main();

        clsStores[1] = new ScanResultStore();
        clsStores[1] = new ScanResultStore();
        results = new ArrayList<ScanResult>();

        ScanResult result4 = new ScanResult();
        result3.BSSID="AD-DG-CF-ED-TF";
        result3.RSSI=1;
        result3.level=5;
        results.add(result4);

        clsStores[1].setScanResult(results);

        clsStores[1].main();
    }

    public static void main2(String[] args)
    {
        ScanResultStore clsStore = new ScanResultStore();
        List<ScanResult> results = new ArrayList<ScanResult>();

        ScanResult result1 = new ScanResult();
        result1.BSSID="AF-DF-CF-ED-EF";
        result1.RSSI=2;
        result1.level=4;
        results.add(result1);
        ScanResult result2 = new ScanResult();
        result2.BSSID="AD-DF-CF-ED-EF";
        result2.RSSI=1;
        result2.level=3;
        results.add(result2);
        ScanResult result3 = new ScanResult();
        result3.BSSID="AD-DG-CF-ED-EF";
        result3.RSSI=2;
        result3.level=5;
        results.add(result3);

        clsStore.setScanResult(results);

        clsStore.main();

        clsStore = new ScanResultStore();
        results = new ArrayList<ScanResult>();

        ScanResult result4 = new ScanResult();
        result3.BSSID="AD-DG-CF-ED-TF";
        result3.RSSI=1;
        result3.level=5;
        results.add(result4);

        clsStore.setScanResult(results);

        clsStore.main();
    }
}

class ScanResult {
    public String BSSID; // MAC address of the AP
    public int RSSI; // RSSI value in dBm
    public int level;
}

class ScanResultStore {
    private List<ScanResult> results; // A list of ScanResult is given by
                                        // system
    private ArrayList<ScanObject> scanObjectList = new ArrayList<ScanObject>();
    private ArrayList<ScanInfo> listOfScanInfoObjects = new ArrayList<ScanInfo>();
    private int index = 0;
    ScanInfo scanInfo = null;

    public void setScanResult(List<ScanResult> results)
    {
        this.results = results;
    }

    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();
    }

    // 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 scanObject = new ScanObject(mac, rssi);
            scanObjectList.add(i, scanObject);
        }

        scanInfo = new ScanInfo(systemTime, scanObjectList);
    }
}

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;
    }
}

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;
    }
}