我有两节课。
在 EvidenceBox 类中,我有一个名为
getParticularEvidence()
的方法
public ArrayList <Evidence> getParticularEvidence(int caseNumber, String evidenceType) {
EvidenceBox e = new EvidenceBox(caseNumber, evidenceType);
return e.getEvidenceByType(caseNumber, evidenceType);
}
此方法的作用是将 caseNumber 和evidenceType 作为参数。在此方法中,我们创建 EvidenceBox 类的对象实例,以便我可以调用 EvidenceBox 类中另一个名为 getEvidenceByType() 的方法。这个方法的重点是获取上面的两个参数并将其传递给另一个类中的另一个方法,该类在 for 循环中有一个 if 语句,用于检查参数是否等于 ArrayList 中名为evidenceBoxList 的内容,如果相等则返回相等的证据列表。
当我在主类中调用它时:
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);
时,它都会创建此列表的一个新的空白实例,其中没有任何值。
接下来,您将迭代此列表并尝试使用以下代码片段添加值:
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 */
现在,它应该返回包含一个对象的列表。