我是Java的新手,将为您提供一点帮助。
有关我的代码的注意事项
我有两节课。
在EvidenceBox类中,我有一个名为getParticularEvidence()的方法。
public ArrayList <Evidence> getParticularEvidence(int caseNumber, String evidenceType) {
EvidenceBox e = new EvidenceBox(caseNumber, evidenceType);
return e.getEvidenceByType(caseNumber, evidenceType);
}
该方法正在做的是将caseNumber和证据类型作为参数。在此方法内部,我们正在创建EvidenceBox类的Object实例,以便我可以调用EvidenceBox类中的另一个名为getEvidenceByType()的方法。该方法的重点是将上面的两个参数传递给另一个类中的另一个方法,该类在for循环内具有if语句,该语句检查参数是否等于称为证据框列表的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添加到ArrayList,列表的返回值始终为空。我该如何解决这个问题?
getEvidence()是返回证据框列表的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()
方法将该空白列表返回给您。在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 */
现在,它应该返回带有一个对象的列表。