这个比较器如何工作? [英] How does this comparator work?
问题描述
package vehicles_order;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
公共类车辆实现类似{
String Vehicle;
String Make;
字符串类型;
double Cost;
public static void main(String [] args){
ArrayList cars = new ArrayList();
cars.add(新车(CAR,Peugot,3008,12500.00));
cars.add(新车(CAR,BMW,316,4995.00));
cars.add(新车(CAR,Ford,Fiesta,2995.00));
Collections.sort(cars);
迭代器itr = cars.iterator();
while(itr.hasNext()){
Object element = itr.next();
System.out.println(element +\\\
);
}
}
公共车辆(String vehicle,String make,String type,double cost){
Vehicle =
Make = make;
Type = type;
Cost = cost;
}
public String toString(){
returnVehicle:+ Vehicle +\\\
+Make:+ Make +\\\
类型:+类型+\\\
+成本:+成本;
}
public int compareTo(Object o1){
if(this.Cost ==((vehicles)o1).Cost)
return 0;
else if((this.Cost)>((vehicles)o1).Cost)
return 1;
else
return -1;
}
}
我的比较器在底部,我只是想知道它实际上工作。假设它像一个堆栈,当它返回1它向上移动堆栈,如果其-1向下移动。
此外,任何人都可以告诉我如何id去排序汽车通过不同的方法。例如,将成本存储在临时最高值中,并检查新值是否高于当前值。
什么是一个比较接口?它是一个包含单一方法的接口:
compareTo(T o)
pre>
提供Java与其他任何对象进行对比的能力。基本上你想比较你的对象与同一类(类)。因此,通常在 compareTo 实现中检查:
if(o instanceof vehicles){
Vehicles v =(Vehicles)o;
//比较
} else {
return false;
}
然后,在比较部分,您应该根据您的业务规则,
- 0 - 如果它们相等;
- 1 - 如果您的对象更大;
- -1 - 如果您的对象较小。
$ b b
这么简单!
package vehicles_order;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class vehicles implements Comparable {
String Vehicle;
String Make;
String Type;
double Cost;
public static void main(String[] args){
ArrayList cars = new ArrayList();
cars.add(new vehicles ("CAR","Peugot","3008",12500.00));
cars.add(new vehicles ("CAR","BMW","316",4995.00));
cars.add(new vehicles ("CAR","Ford","Fiesta",2995.00));
Collections.sort(cars);
Iterator itr = cars.iterator();
while(itr.hasNext()){
Object element = itr.next();
System.out.println(element + "\n");
}
}
public vehicles(String vehicle, String make, String type, double cost){
Vehicle = vehicle;
Make = make;
Type = type;
Cost = cost;
}
public String toString() {
return "Vehicle: " + Vehicle + "\n" + "Make: " + Make + "\n" + "Type: " + Type + "\n" + "Cost: " + Cost;
}
public int compareTo(Object o1) {
if (this.Cost == ((vehicles) o1).Cost)
return 0;
else if ((this.Cost) > ((vehicles) o1).Cost)
return 1;
else
return -1;
}
}
My comparator is at the bottom and I just wondering how it actually works. Im guessing its like a stack, where when it returns 1 it moves up the stack if its -1 moves down.
Also, can anyone tell me how id go about ordering the cars via a different method. For example, store the cost in a temp highest value and check if the new value is higher than the current one
What is a Comparable interface? It is an interface containing single method:
compareTo(T o)
providing capability for Java to comprare your object with any other. Basically you would like to compare your object with those of the same kind (class). So there is a usual check in compareTo implementation:
if (o instanceof vehicles) {
Vehicles v = (Vehicles)o;
// compare
} else {
return false;
}
Then, in compare section you should decide, based on your business-rules, whether other vehicle is equals, more of less to your object.
- 0 - if they are equal;
- 1 - if your object is greater;
- -1 - if your object is lesser.
All that simple!
这篇关于这个比较器如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!