简体   繁体   中英

How to compare object-attributes, stored in ArrayList?

I´ve created a method, which saves some objects in an ArrayList in mainActivity. 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.

1) How can i compare only the object attribute "box"? 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.

Related method in 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):

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:


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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM