Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在不使用';可比';_Java_Sorting_Arraylist - Fatal编程技术网

Java 如何在不使用';可比';

Java 如何在不使用';可比';,java,sorting,arraylist,Java,Sorting,Arraylist,这是我的数组 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)); 我想要一种从最便宜的开始排序的方法,但我不想用比较的方法。可

这是我的数组

 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));
我想要一种从最便宜的开始排序的方法,但我不想用比较的方法。可能创建一个临时变量,并用值检查此变量,如果值便宜,则覆盖这些值

我试过这个

public static void main(String[] args){

    ArrayList carsArray = new ArrayList();
    carsArray.add(new vehicles ("CAR","Peugot","3008",12500.00));
    carsArray.add(new vehicles ("CAR","BMW","316",4995.00));
    carsArray.add(new vehicles ("CAR","Ford","Fiesta",2995.00));


    vehicles lowestPrice = (vehicles) carsArray.get(0);
    for(vehicles car : carsArray){
        if(car.Cost<lowestPrice.Cost){
            lowestPrice = car;
        }
    }

}
publicstaticvoidmain(字符串[]args){
ArrayList carsArray=新的ArrayList();
添加(新车(“汽车”、“标致”、“3008”、12500.00”);
添加(新车(“汽车”、“宝马”、“316”、4995.00”);
添加(新车(“汽车”、“福特”、“嘉年华”,2995.00));
车辆最低价格=(车辆)carsArray.get(0);
车辆(汽车:carsArray){
if(car.Cost
car lowerstprice=cars.get(0);
用于(汽车:汽车){

如果(汽车价格如果您想使用OP中提到的方法,您可以为
车辆
类别声明
静态
变量,并在
承包商
中检查价格是否低于实际最低价格 但我的建议是使用
比较器
可比
,因为这是它们的自然用法。对象的值也可以在各种
集合
方法中更改,您必须注意这一点

如果你还想做,你就这样做

public static int min\u price=Integer.MAX\u VALUE;

然后在
构造函数中
方法
if(price您可以使用一个树形图,其中price作为键,vehicles对象作为值。然后它将按price排序,并且您将拥有车辆的所有信息作为值。

如果您想对列表中的项目进行排序,可以使用
Collections.sort(List List,ComparatorSure。当您尝试您提到的方法时发生了什么?老实说,我不想使用比较器,还有其他方法吗?您最终将使用与
集合相同的逻辑。sort
(或其他排序算法)和
Comparator
。只需准确地使用您所说的:使用temp
Car
,它可以记住当前最低值并遍历您的数组列表。这是一种奇怪的方法,但会起作用。@AJ_91或更好的方法是保持最低的价格指数。我如何在我的代码中实现这一点?我是个傻瓜,所以我不完全理解。我犯了一个错误。T他的“汽车”在下面划线red@AJ_91
Car
在您的情况下应该是
vehicles
。顺便说一句,类名应该以大写字母开头(例如
vehicles
),因为类似乎代表一辆车,所以最好将其命名为
vehicles
。如果您有机会查看,我在我的问题中添加了一些代码:)我更新了我的答案…顺便说一句,如果你告诉我们有什么错误,而不是仅仅告诉我们有错误,我们不会花很多时间来找到解决方案。stacktrace值千言万语。它有效:))非常感谢,很抱歉回复太晚。银行假日周末等等;)
Car lowestPrice = cars.get(0);
for(Car car : cars) {
   if(car.price<lowestPrice.price) {
       lowestPrice = car;
   }
}
ArrayList<Vehicles> yourlist = new ArrayList<Vehicles>();
class VechiclePriceComparator implements Comparator<vechiles> {

  public int compare(vechiles v1, vechiles v2) {

    if(v1 == v2) {
       return 0;
    )

    return Double.compare(v1.getPrice(),v2.getPrice());

  }

}