Arraylist of objects - smallest value

天涯浪子 提交于 2019-12-14 02:36:13

问题


I have an arraylist of objects, and the objects have "double" values. I need to print out all the objects, and have the object with the highest value printed first, and the object with the lowest value last.

I have also tried to use Collections to sort the arraylist and then print, but I can't seem to get it to work.

My arraylist is:

ArrayList<Transition> transitions = new ArrayList<Transition>();

When i tried to used Collections i used:

Object minValue = Collections.min(transitions);

Hope some of you can help :)


回答1:


If Transition object implemented Comparable interface try this way. It should work.

 Collections.sort(transitions);
 Transition transition = transitions.get(0);



回答2:


Just because Transition HAS double values, doesn't make Transition comparable. You must implement a Comparator or define Transition as implements Comparable then implement the comparTo() method. You can compare the two object by that double value. Then you can use the sort() and min()

Java does not know how to compare the object unless you tell it how the object are comparable. Some classes come comparable, like String class, Date, Integer, etc.

Edit: To make Transition Comparable

public class Transition implements Comparable<Transition> {
    double value;

    public Transition(double value){
        this.value = value;
    }

    public int compareTo(Transition t){
        if (this.value > t.value) {  // your comparing the Transitionss by the 'value'
            return 1;
        }
        else if (this.value == t.value){
            return 0;
        }
        else {
            return -1;
        }
    }
}

class SomeClass {
    public static void main(String [] args){
        Transition tranny1 = new Transition(5);
        Transition tranny2 = new Transition(6);

        ArrayList<Transition> trannys = new ArrayList<>();

        trannys.add(tranny1);
        trannys.add(tranny2);

        Collections.sort(trannys);
        double lowTrannyValue = trannys.get(0).value;
    }
}



回答3:


List<Transition> transitions = new ArrayList<Transition>();    
//fill list
Collections.sort(transitions, new Comparator<Transition>() {
                @Override
                public int compare(Transition lhs, Transition rhs) {
                    if (lhs.doubleValue > rhs.doubleValue)
                       return 1;
                    if (lhs.doubleValue < rhs.doubleValue)
                       return -1;
                    return 0;
                }
            });

System.out.print(transitions.get(0)); // smallest value



回答4:


As others have said, if you want to use Collections.sort(), you must have Transition implement the Comparable interface, which means including a .compareTo() and modifying your class header to:

public class Transition implements Comparable<Transition>

Alternatively, you could do this:

Collections.sort(transitions, new Comparator<Transition>() {
    @Override
    public int compareTo(Transition t1, Transition t2) {
          return t1.getDoubleValue().compareTo(t2.getDoubleValue());
    }
});

where getDoubleValue() is just a name that I used to represent a method in your Transition class that returns the Double that you referenced in your post. If you don't want to use Double, then you could write .compareTo() as follows (NB: uses conditional ternary operator in lieu of if statements):

@Override
public int compareTo(Transition t1, Transition t2) {
      return t1.getDoubleValue() > t2.getDoubleValue() ? 1 : 
             t1.getDoubleValue() < t2.getDoubleValue() ? -1 : 0;
}

Here's a reference for Object Ordering in Java.



来源:https://stackoverflow.com/questions/19970400/arraylist-of-objects-smallest-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!