[英]How to compare object-attributes, stored in ArrayList?
I´ve created a method, which saves some objects in an ArrayList in mainActivity.我创建了一个方法,它将一些对象保存在 mainActivity 的 ArrayList 中。 Which objects are saved is controlled via the variable "category".保存哪些对象是通过变量“类别”控制的。
After doing that i want to compare the ObjectsDEGR attribute "box" with the numbers 1,2 und 3 and depending on that, divide the object in 3 new ArrayLists.之后,我想将 ObjectsDEGR 属性“框”与数字 1,2 和 3 进行比较,并据此将 object 划分为 3 个新的 ArrayList。
1) How can i compare only the object attribute "box"? 1) 我怎样才能只比较 object 属性“框”? I only managed to get the object by it´s index, inside of the ArrayList, but it´s not the purpose to compare the whole object with the numbers.我只设法通过索引获得 object,在 ArrayList 内部,但这并不是将整个 object 与数字进行比较的目的。
Related method in mainActivity: 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));
}
}
}
The object, which needs to be saved (except the transient attributes):需要保存的object(暂态属性除外):
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;}
}
You could try to implement the Comparable interface:您可以尝试实现 Comparable 接口:
public class ObjektsDEGR implements Serializable, Comparable<ObjektsDEGR>{
and you have to add the compare method:你必须添加比较方法:
@Override
public int compareTo(ObjektsDEGR o) {
if(this.getBox() < o.getBox()) {
return -1;
}else if(this.getBox() > o.getBox()) {
return 1;
}else{
return 0;
}
}
If you want to sort the list you can use then:如果要对列表进行排序,则可以使用:
Collections.sort(box1Filtered);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.