如果我从另一个类调用它,为什么我的ArrayList为空?

时间:2016-01-30 17:13:22

标签: java class arraylist methods parameter-passing

我对Java很陌生,小小的帮助将受到高度赞赏。

关于我的代码的事情

我有两节课。

  1. First Class名为EvidenceBox.class
  2. 第二类叫做Police.class
  3. 在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()是getter方法,它返回evidenceBox列表。

    这是我的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;
        }
    }

1 个答案:

答案 0 :(得分:2)

evidenceBoxListEvidenceBox类的私有。

每当你执行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()方法会将此空白列表返回给您。

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

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

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