java – 为什么我从另一个类调用它时,我的ArrayList是空的?

我对
Java很新,一个小小的帮助将受到高度赞赏.

关于我的代码的事情

我有两节课.

> First Class称为EvidenceBox.class
>第二类叫做Police.class

在EvidenceBox类中,我有一个名为getParticularEvidence()的方法

    public ArrayList <Evidence> getParticularEvidence(int caseNumber, String evidenceType) {
      EvidenceBox e = new EvidenceBox(caseNumber, evidenceType);
      return e.getEvidenceByType(caseNumber, evidenceType);
    }

这个方法正在做的是它将caseNumber和evidenceType作为参数.在这个方法中,我们创建了一个EvidenceBox类的Object实例,这样我就可以调用另一个名为getEvidenceByType()的方法,该方法位于EvidenceBox类中.这个方法的重点是将上面的这两个参数传递给另一个类中的另一个方法,该方法在for循环中有一个if语句,用于检查参数是否等于一个名为evidenceBoxList的ArrayList中的内容,如果它等于那么返回相同的证据清单.

当我在Main类中调用它时:

System.out.println(crimeBoxOne.getEvidenceByType(2005000381, "S.Avery"));

它的作用是,它转到getEvidenceByType方法,然后调用Evidence类中的另一个方法:

    public ArrayList < Evidence > getEvidenceByType(int keyNumber, String keyName) {
      ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
      for (Evidence e: setOfEvidence) {
        if (this.getCaseNumber() == keyNumber) {
          if (this.getCaseName().equals(keyName)) {
            System.out.println("Pieces of DNA Evidence in Evidence Box number " + this.getCaseNumber() + " found by " + this.getCaseName() + ": " + this.getEvidence());
            getEvidence().add(e);
          } else {
            System.out.println("Not found!");
          }
        } else {
          System.out.println("Not Found!");
        }
      }
      return getEvidence();
    }

getCaseNumber是EvidenceBox的编号,getCaseName是evidenceBox的名称

我的问题

我的问题是列表的返回始终为空,即使不是因为我将EvidenceBox添加到ArrayList.我该如何解决这个问题?

getEvidence()是返回evidenceBox列表的getter方法.

这是我的EvidenceBox类:

package looselycoupled;

import java.util.*;

public class EvidenceBox {
  private int caseNumber;
  private String caseName;
  private ArrayList evidenceBoxList;

  public EvidenceBox(int caseNumber, String caseName) {
    this.evidenceBoxList = new ArrayList < Evidence > ();
    this.caseNumber = caseNumber;
    this.caseName = caseName;
  }

  public ArrayList < Evidence > getEvidenceByType(int keyNumber, String keyName) {
    ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
    for (Evidence e: setOfEvidence) {
      if (this.getCaseNumber() == keyNumber) {
        if (this.getCaseName().equals(keyName)) {
          System.out.println("Pieces of DNA Evidence in Evidence Box number " + this.getCaseNumber() + " found by " + this.getCaseName() + ": " + this.getEvidence());
          getEvidence().add(e);
        } else {
          System.out.println("Not found!");
        }
      } else {
        System.out.println("Not Found!");
      }
    }
    return getEvidence();
  }

  public void add(Evidence getTypeFromEvidenceClass) {
    evidenceBoxList.add(getTypeFromEvidenceClass);
  }


  public ArrayList < Evidence > getEvidence() {
    return evidenceBoxList;
  }



  public void printRecordsOfTheEvidence() {
    Set < Evidence > setOfEvidence = new HashSet < > (evidenceBoxList);
    for (Evidence e: setOfEvidence) {
      System.out.println(Collections.frequency(evidenceBoxList, e) + "x " + e);
    }
  }

  public int getCaseNumber() {
    return caseNumber;
  }

  public String getCaseName() {
    return caseName;
  }

  @
  Override
  public String toString() {
    return caseNumber + " " + caseName;
  }
}

和警察班:

package looselycoupled;

import java.util.ArrayList;

public class Cop {
    private String name;
    //private ArrayList collectionOfEvidences = new ArrayList<>();
    private ArrayList<EvidenceBox> collectionOfEvidences = new ArrayList<>();
    public Cop(String name) {
        this.name = name;
    }

    public void copTakesAnEvidenceBox(EvidenceBox box) {
        collectionOfEvidences.add(box);
    }

    public ArrayList<Evidence> getParticularEvidence(int caseNumber, String evidenceType) {
        EvidenceBox e = new EvidenceBox(caseNumber, evidenceType);
        return e.getEvidenceByType(caseNumber, evidenceType);
    }

    public ArrayList<EvidenceBox> getEvidence() {
        return collectionOfEvidences;
    }

    public String toString() {
        return name;
    }
}

最佳答案 evidenceBoxList是EvidenceBox类的私有.

每当你做EvidenceBox e = new EvidenceBox(caseNumber,evidenceType);它正在创建此列表的新Blank实例,没有值.

接下来,您将迭代此列表并尝试使用以下代码添加值:

ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
    for (Evidence e: setOfEvidence) {
      if (this.getCaseNumber() == keyNumber) {
        if (this.getCaseName().equals(keyName)) {
          System.out.println("Pieces of DNA Evidence in Evidence Box number " + this.getCaseNumber() + " found by " + this.getCaseName() + ": " + this.getEvidence());
          getEvidence().add(e);
        } else {
          System.out.println("Not found!");
        }
      } else {
        System.out.println("Not Found!");
      }
    }
    return getEvidence();
  }

既然你最初在evidenceBoxList中没有元素,它将永远不会进入for循环,永远不会调用add()方法.

然后getEvidence()方法将此空白列表返回给您.

添加getEvidence().add(new Evidence(keyNumber,keyName));在for循环之前验证问题.

ArrayList < Evidence > setOfEvidence = new ArrayList < > (evidenceBoxList);
    /* Add Here */
    getEvidence().add(new Evidence(keyNumber,keyName));
    for (Evidence e: setOfEvidence) {
        /* Rest of the code */

现在,它应该返回带有一个对象的列表.

点赞