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 选择排序方法';我将按类型对ArrayList排序_Java_Sorting_Arraylist_Selection Sort - Fatal编程技术网

Java 选择排序方法';我将按类型对ArrayList排序

Java 选择排序方法';我将按类型对ArrayList排序,java,sorting,arraylist,selection-sort,Java,Sorting,Arraylist,Selection Sort,SOF。我有个问题,我有点困难 下面的代码应该逐行筛选文件,有效地利用StringTokenizer来获取汽车类的Make、Model、Year和miliner(按该顺序),并将它们存储在汽车对象中,然后将其添加到2个ArrayList中,其中一个按Make“排序”,另一个按“未排序” 我编写的selectionSort最初使用的字符串由于明显的原因不起作用 这可以通过使selectionSort与对象(汽车)一起工作来解决吗?Eclipse向我推荐了这一点,而当前的selectionSort就

SOF。我有个问题,我有点困难

下面的代码应该逐行筛选文件,有效地利用StringTokenizer来获取汽车类的Make、Model、Year和miliner(按该顺序),并将它们存储在汽车对象中,然后将其添加到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;
  }
}
public void readFile(String file)抛出FileNotFoundException//一个接受字符串文件的对象数组
{  
试一试{
File myFile=new File(File);//将参数字符串转换为文件
Scanner Scanner=new Scanner(myFile);//文件允许我们使用Scanner
String line=scanner.nextLine();//读取当前行并指向下一行
StringTokenizer tokenizer=新的StringTokenizer(行,“”;//标记该行,即扫描的文件
int tokenCount=new StringTokenizer(第,“”,“”)行。countTokens();//对令牌进行计数
while(tokenizer.hasMoreTokens()){
如果(令牌计数>4){
System.out.println(“非4个代币”);
}
否则{
字符串CarMake=tokenizer.nextToken();//因为汽车是按品牌、型号、年份和里程排序的
字符串CarModel=tokenizer.nextToken();
int CarYear1=Integer.parseInt(tokenizer.nextToken());
int CarMileage1=Integer.parseInt(tokenizer.nextToken());//将字符串数字转换为整数
Car cars=新车(CarMake、CarModel、CarYear1、CarMileage1);//因为汽车有固定的订单
arraylist.add(cars);//将汽车添加到未排序的数组中
}
}
scanner.close();//关闭扫描仪
}捕获(FileNotFoundException f){
f、 printStackTrace();
}
arraylist2.addAll(arraylist);
选择排序(ArrayList 2);
}
公共静态void selectionSort(ArrayList ArrayList)//使用字符串进行选择排序
{

对于(int i=0;i您的新代码不起作用的原因是您只交换了汽车的品牌,而不是完整的
Car
对象

应该是:

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

与您的问题无关,但我也建议您使用另一种排序算法,如果您的用例中可能/允许的话。选择排序是最糟糕的排序算法之一,尽管它很容易实现。嗯,您推荐什么?那么,类似Collections.sort(arraylist2,null)的东西?这可以按照字典顺序对Make对象进行排序吗?这可以作为selectionSort的替代方法吗?使用
null
作为第二个参数将不起作用,因为您需要一个检查Make值的比较器。假设您的car类中有一个getter方法
getMake()
,您可以使用
List.sort(…)
使用
Comparator。比较(…)
和表达式lambda以从汽车对象提取make:
arrayList2.sort(Comparator.comparating(Car::getMake));
此外,如果我的回答对您的问题有帮助,请接受我的回答,我将不胜感激。