【问题标题】:selectionSort method that'll sort an ArrayList by typeselectionSort 方法,将按类型对 ArrayList 进行排序
【发布时间】:2019-08-07 02:47:21
【问题描述】:

SOF。我有一个问题,我遇到了一些困难。

下面的代码应该逐行筛选文件,有效地利用 StringTokenizer 来获取 Car 类的 Make、Model、Year 和 Mileage(按此顺序)并将它们存储在汽车对象中,然后我添加到 2 个 ArrayList,其中一个由 Make“排序”,另一个是“未排序”。

我编写的 selectionSort 最初使用字符串,但由于明显的原因无法正常工作。

可以通过使 selectionSort 与对象(汽车)一起工作来解决这个问题吗? Eclipse 向我推荐了它,而当前的 selectionSort 就是它的产物。

public void readFile(String file) throws FileNotFoundException //an object array that takes in string files
{  
   try {
        File myFile = new File(file); //converts the parameter string into a file
        Scanner scanner = new Scanner(myFile); //File enables us to use Scanner
        String line = scanner.nextLine(); //reads the current line and points to the next one
        StringTokenizer tokenizer = new StringTokenizer(line, ","); //tokenizes the line, which is the scanned file

        int tokenCount = new StringTokenizer(line, ",").countTokens(); //counts the tokens 
        while (tokenizer.hasMoreTokens()){
            if(tokenCount > 4) {
               System.out.println(" Not 4 tokens");
            }
            else {
               String CarMake = tokenizer.nextToken(); //since car is in order by make, model, year, and mileage
               String CarModel = tokenizer.nextToken();
               int CarYear1 = Integer.parseInt(tokenizer.nextToken());
               int CarMileage1 = Integer.parseInt(tokenizer.nextToken()); //converts the String numbers into integers
               Car cars = new Car(CarMake, CarModel, CarYear1, CarMileage1); //since the car has a fixed order 
               arraylist.add(cars); //add the cars to the unsorted array
            }
        }
        scanner.close(); //close the scanner  
    } catch (FileNotFoundException f){
        f.printStackTrace();
    }
    arraylist2.addAll(arraylist);
    selectionSort(arraylist2);
}

public static void selectionSort(ArrayList<Car> arraylist) //Selection sort using strings
{
  for (int i = 0; i <= arraylist.size(); i++)
  {
    // Look through the unsorted strings (those at j or higher) for the one that is first in order
    int min = i;
    for (arraylist[i].getMake.compareTo(arraylist[min].getMake) < 0) { //use the inherent string compareTo
          min = k;  
    }
    String temp = arraylist[i].getMake; //swapping
    arraylist[i].getMake = arraylist[min].getMake;
    arraylist[min] = temp;
  }
}

任何帮助将不胜感激。

【问题讨论】:

    标签: java sorting arraylist selection-sort


    【解决方案1】:

    您的新代码不起作用的原因是您只交换了汽车的品牌,而不是完整的 Car 对象。

    应该是:

    Car temp = arraylist[i]; //swapping
    arraylist[i] = arraylist[min];
    arraylist[min] = temp;
    

    【讨论】:

    • 与您的问题无关,但如果可能/在您的用例中允许,我也建议您使用另一种排序算法。选择排序是最差的排序算法之一,尽管它很容易实现。
    • 嗯,你有什么推荐的?
    • 所以,像 Collections.sort(arraylist2, null);??这适用于按字典顺序对 Make 对象进行排序吗?这可以有效地用作 selectionSort 的替代品吗?
    • 使用null 作为第二个参数是行不通的,因为你需要一个比较器来检查make 值。假设您的汽车类中有一个 getter 方法 getMake(),您可以使用 List.sort(…)Comparator.comparing(…) 和一个表达式 lambda 从 Car 对象中提取品牌:arrayList2.sort(Comparator.comparing(Car::getMake));
    • 另外,如果您能接受我的回答,如果它对您的问题有所帮助,我将不胜感激。
    猜你喜欢
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2021-12-22
    • 1970-01-01
    • 2017-05-22
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多