如何解决如何比较存储在 ArrayList 中的对象属性?
我创建了一个方法,它将一些对象保存在 mainActivity 的 ArrayList 中。 保存哪些对象是通过变量“category”控制的。
这样做之后,我想将 ObjectsDEGR 属性“box”与数字 1,2 和 3 进行比较,并根据它,将对象划分为 3 个新的 ArrayList。
1) 如何只比较对象属性“box”?我只设法通过它在 ArrayList 内的索引来获取对象,但这并不是将整个对象与数字进行比较的目的。
mainActivity中的相关方法:
private void writeToArrayList(int category){
ArrayList<ObjektsDEGR> catchAllBoxes = new ArrayList<>();
for (int j = ((category-1)*nV); j < nV+((category-1)*nV); j++) {
catchAllBoxes.add(basicDEGR[j]);
}
ArrayList<ObjektsDEGR> box1Filtered = new ArrayList<>();
ArrayList<ObjektsDEGR> box2Filtered = new ArrayList<>();
ArrayList<ObjektsDEGR> box3Filtered = new ArrayList<>();
for (int j = ((category-1)*nV); j < nV+((category-1)*nV); j++){
if (catchAllBoxes.get(j).equals(1)) {
box1Filtered.add(catchAllBoxes.get(j));
}
else if (catchAllBoxes.get(j).equals(2)) {
box2Filtered.add(catchAllBoxes.get(j));
}
else if (catchAllBoxes.get(j).equals(3)) {
box3Filtered.add(catchAllBoxes.get(j));
}
else {
box3Filtered.add(catchAllBoxes.get(j));
}
}
}
需要保存的对象(瞬态属性除外):
import java.io.Serializable;
public class ObjektsDEGR implements Serializable {
private int index;
private String voc;
private transient String vocF1;
private transient String vocF2;
private transient String vocF3;
private transient String vocF4;
private transient String vocF5;
private int box;
//+++++++++++ KONSTRUKTOR +++++++++++++
public ObjektsDEGR(int voc,int index,int vocF1,int vocF2,int vocF3,int vocF4,int vocF5,int box){
this.index = index;
this.voc = Integer.toString(voc);
this.vocF1 = Integer.toString(vocF1);
this.vocF2 = Integer.toString(vocF2);
this.vocF3 = Integer.toString(vocF3);
this.vocF4 = Integer.toString(vocF4);
this.vocF5 = Integer.toString(vocF5);
this.box = box;
}
//+++++ GETTER ++++++
public int getIndex(){
return index;
}
public String getVoc() {
return voc;
}
public String getVocF1() {
return vocF1;
}
public String getVocF2() {
return vocF2;
}
public String getVocF3() {
return vocF3;
}
public String getVocF4() {
return vocF4;
}
public String getVocF5() {
return vocF5;
}
public int getBox(){
return box;
}
//++++ SETTER +++++
public void setIndex(int index){
this.index = index;
}
public void setVoc(int voc){
this.voc = Integer.toString(voc);
}
public void setVocF1(int vocF1){
this.voc = Integer.toString(vocF1);
}
public void setVocF2(int vocF2){
this.voc = Integer.toString(vocF2);
}
public void setVocF3(int vocF3){
this.voc = Integer.toString(vocF3);
}
public void setVocF4(int vocF4){
this.voc = Integer.toString(vocF4);
}
public void setVocF5(int vocF5){
this.voc = Integer.toString(vocF5);
}
public void setBox (int box){ this.box = box;}
}
解决方法
您可以尝试实现 Comparable 接口:
public class ObjektsDEGR implements Serializable,Comparable<ObjektsDEGR>{
并且您必须添加比较方法:
@Override
public int compareTo(ObjektsDEGR o) {
if(this.getBox() < o.getBox()) {
return -1;
}else if(this.getBox() > o.getBox()) {
return 1;
}else{
return 0;
}
}
如果你想对列表进行排序,你可以使用:
Collections.sort(box1Filtered);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。