简体   繁体   English

无法弄清楚为什么对象的 ArrayList 没有正确排序

[英]Can't figure out why is ArrayList of objects not sorted properly

I'm working on an exchange sort for an ArrayList of objects so that it can be sorted ascending depending on the value in the object.我正在为对象的 ArrayList 进行交换排序,以便可以根据 object 中的值对它进行升序排序。 When I compile it, everything works great until the last element is not sorted properly.当我编译它时,一切都很好,直到最后一个元素没有正确排序。 I suspect that the problem is with the iterator.我怀疑问题出在迭代器上。 I tried many combinations but it won't sort properly.我尝试了许多组合,但无法正确排序。 Also, I can't use sort() or any prebuilt libraries.另外,我不能使用 sort() 或任何预建库。 I'd appreciate any guidance!我将不胜感激任何指导!

void sortAndDisplay (ArrayList<Car> cars) {

    Car temp;

    for (int i = 0; i < cars.size() -1; i++) {
        for (int j = i+1; j < cars.size(); j++ ) {
            if (((cars.get(i).getMake()).compareToIgnoreCase(cars.get(j).getMake()) > 0)) {
                temp = cars.get(i);
                cars.set(i, cars.get(j));
                cars.set(j, temp);
            }


        }
    }


      for (int i = 0; i < cars.size(); i++) { 
          System.out.print(cars.get(i)+"\n\n");

      }


}

The Car class:汽车 class:

public class Car {
String make;
String model;


public Car (String make,
            String model)   {

    this.make = make;
    this.model = model;

}

public String getMake() {
    return make;

}

The method I used to read the values from the file:我用来从文件中读取值的方法:

ArrayList<Car> readCars(FileReader file) throws Exception  {
    String arr[] = {};
    String line = "";
    BufferedReader scan = new BufferedReader(file);
    ArrayList<Car> carsArr = new ArrayList<Car>();

    while ((line = scan.readLine()) != null) {
        arr = line.split(","); 
        Car car = new Car(arr[0], arr[1]);
        carsArr.add(car);
    }
    System.out.println("The file was read.");
    return carsArr;

}

java has sorting built in: java 内置排序功能:

void sortAndDisplay (List<Car> cars) {
    cars.sort(Comparator.comparing(Car::getMake));
    System.out.println(cars.stream().collect(Collectors.joining("\n\n")));
}

I created the file x.txt with the following data:我使用以下数据创建了文件x.txt

WERTfgd, bmw
ETRHFDdfgsdf, bmw
ewrd, bmw
HTfgdfgs, bmw
dDSFSfgd, bmw
ter, bmw
jhvbn, bmw
Frqw, bmw

I copied your code literally by adding getMake() into this System.out.print (cars.get (i).getMake () + "\ n \ n");我通过将getMake()添加到此System.out.print (cars.get (i).getMake () + "\ n \ n");逐字复制了您的代码in order to get the wanted output...为了得到想要的output...

Output Output

dDSFSfgd

ETRHFDdfgsdf

ewrd

Frqw

HTfgdfgs

jhvbn

ter

WERTfgd

Which is correct, so I can't figure out what's your problem.哪个是正确的,所以我无法弄清楚你的问题是什么。

Maybe you are not sure how the method compareToIgnoreCase() works?也许您不确定compareToIgnoreCase()方法的工作原理?

Tried your code with short example list and it worked.用简短的示例列表尝试了您的代码并且它有效。 Could you give a failing example input with corresponding output?您能否给出一个带有相应 output 的失败示例输入?

BTW I recommend adding a toString() method to the Car class.顺便说一句,我建议向Car class 添加一个toString()方法。 For example:例如:

@Override
public String toString() {
    return "<Car<" + make + "><" + model + ">>";
}

With that printing the list of cars can be done like:通过该打印,汽车列表可以像这样完成:

System.out.println(cars);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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