一个提出的问题尚未得到回答。它甚至没有标记答案。
我真的不知道发生了什么事。我有一个用元素填充的对象列表,但是如果我做indexOf(element),它将一直返回-1。 这些是我要做的检查:
Crime oldCrime = this.service.getCrimeById(319L);
System.out.println("ID old crime " + oldCrime.getId());
System.out.println("Index " + this.service.getAllCrimes().indexOf(oldCrime));
int oldSize = this.service.getAllCrimes().size();
for (Crime c : this.service.getAllCrimes()) {
System.out.println("Crime id " + c.getId());
}
这是输出:
因此列表中存在oldCrime,因为我可以打印其ID,所以存在列表中的所有犯罪,但是在同一列表上调用的indexOf方法返回-1。
也许这是一个愚蠢的错误,但我真的无法弄清楚。
public class Crime {
@CsvBindByName(column = "id")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@CsvBindByName(column = "zipCode")
@NonNull
private int zipCode;
@CsvBindByName(column = "totPopulation")
@NonNull
private int totPopulation;
@CsvBindByName(column = "medianAge")
@NonNull
private double medianAge;
@CsvBindByName(column = "totMales")
@NonNull
private int totMales;
@CsvBindByName(column = "totFemales")
@NonNull
private int totFemales;
@CsvBindByName(column = "totHouseholds")
@NonNull
private int totHouseholds;
@CsvBindByName(column = "avgHouseholdSize")
@NonNull
private double avgHouseholdSize;
/**
* @param id
* @param zipCode
* @param totPopulation
* @param medianAge
* @param totMales
* @param totFemales
* @param totHouseholds
* @param avgHouseholdSize
*/
public Crime(long id, int zipCode, int totPopulation, double medianAge, int totMales, int totFemales,
int totHouseholds, double avgHouseholdSize) {
this.id = id;
this.zipCode = zipCode;
this.totPopulation = totPopulation;
this.medianAge = medianAge;
this.totMales = totMales;
this.totFemales = totFemales;
this.totHouseholds = totHouseholds;
this.avgHouseholdSize = avgHouseholdSize;
}
/**
*
*/
public Crime() {
this.id = 0;
this.zipCode = 0;
this.totPopulation = 0;
this.medianAge = 0;
this.totMales = 0;
this.totFemales = 0;
this.totHouseholds = 0;
this.avgHouseholdSize = 0;
}
/**
* @param id
*/
public Crime(long id) {
this.id = id;
this.zipCode = (int)id;
this.totPopulation = (int)id;
this.medianAge = (int)id;
this.totMales = (int)id;
this.totFemales = (int)id;
this.totHouseholds = (int)id;
this.avgHouseholdSize = (int)id;
}
public long getId() {
return id;
}
public int getZipCode() {
return zipCode;
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
public int getTotPopulation() {
return totPopulation;
}
public void setTotPopulation(int totPopulation) {
this.totPopulation = totPopulation;
}
public double getMedianAge() {
return medianAge;
}
public void setMedianAge(double medianAge) {
this.medianAge = medianAge;
}
public int getTotMales() {
return totMales;
}
public void setTotMales(int totMales) {
this.totMales = totMales;
}
public int getTotFemales() {
return totFemales;
}
public void setTotFemales(int totFemales) {
this.totFemales = totFemales;
}
public int getTotHouseholds() {
return totHouseholds;
}
public void setTotHouseholds(int totHouseholds) {
this.totHouseholds = totHouseholds;
}
public double getAvgHouseholdSize() {
return avgHouseholdSize;
}
public void setAvgHouseholdSize(double avgHouseholdSize) {
this.avgHouseholdSize = avgHouseholdSize;
}
}
答案 0 :(得分:4)
indexOf
根据equals
方法进行比较。在犯罪类中覆盖equals
,否则将作为参考比较。