简体   繁体   English

使用分隔符对数组列表进行排序

[英]Sorting an array list with a delimiter

I have been stuck on this problem for so long and i have no idea what to do. 我一直坚持这个问题已久,我不知道该怎么做。 Basically i have a text file with people names then student number then prize money like this: 基本上我有一个文本文件,其中包含人名,然后是学号,然后奖金如下:

Green%3243%1000
Kevin%7657%400
Frank%345%10000
Bob%5435%5000
Stefan%31231%1000
Javis%4532%100
IronMan%5435%2000
Lamp%534%3000

What i want to be able to do is sort the array based on the last number. 我想要做的是根据最后一个数字对数组进行排序。

I tried this abomination (Don't bother reading it its garbage): 我试过这种可憎的行为(不要费心阅读它的垃圾):

    boolean flag = true;
    String temp;
    int temp1;
    int temp2;

    while (flag){
        flag = false;
        for(int j=0;  j < list.size() -1;  j++ ){
            System.out.println(list.get(j));
            Scanner s = new Scanner(list.get(j)).useDelimiter("%");
            s.next();
            s.next();
            temp1 = s.nextInt();
            Scanner s2 = new Scanner(list.get(j+1)).useDelimiter("%");
            s2.next();
            s2.next();
            temp2 = s2.nextInt();
            if (temp1 < temp2){
                temp = list.get(j);
                list.add(j, list.get(j+1));
                list.add(j+1,temp);
                flag = true;
            } 
        } 
    }

But its just infinitely looping. 但它只是无限循环。 My though while making it was just patching array lists into a bubble sort. 我虽然在制作它时只是将数组列表修补为冒泡排序。

If anyone has any ideas and is willing to share them it will be greatly appreciated. 如果有人有任何想法并愿意分享它们将非常感激。

Here's something for you to get head started. 这是让你开始的东西。

  1. Create a map for prize money => line as key/value pair 创建奖金地图=>行作为键/值对
  2. Read each line in the file, parse it and put key/value pair in the above map 读取文件中的每一行,解析它并将键/值对放在上面的映射中
  3. Once your map is ready, convert the keys entry set into the collections like list 地图准备就绪后,将键条目集转换为集合之类的列表
  4. Sort the collections, using Collections.sort() 使用Collections.sort()对集合进行排序
  5. Iterate over the created map, and for each value in the collection get the corresponding value from the map. 迭代创建的地图,并为集合中的每个值从地图中获取相应的值。

Hope this helps you to get the workflow. 希望这有助于您获得工作流程。

Java is an object-oriented language, so I'll just use objects: Java是一种面向对象的语言,所以我只使用对象:

  • Create a Student object with the three values you want to store (and a toString() method to print them separated by "%": 使用要存储的三个值创建一个Student对象(以及一个以“%”分隔的toString()方法打印它们:

     public class Student { private final String name; private final int number; private final int prizeMoney; public Student(final String name, final int number, final int prizeMoney) { this.name = name; this.number = number; this.prizeMoney = prizeMoney; } @Override public String toString() { return name+"%"+number+"%"+prizeMoney; } public int getPrizeMoney() { return prizeMoney; } } 
  • Read your lines as Student objects, and store them in a List : 将您的行读作Student对象,并将它们存储在List

     final Scanner scan = new Scanner(new File("/path/to/StudentsList")); final List<Student> students = new ArrayList<Student>(); while (scan.hasNextLine()) { final Scanner line = new Scanner(scan.nextLine()); line.useDelimiter("%"); students.add(new Student(line.next(), line.nextInt(), line.nextInt())); line.close(); } scan.close(); 
  • Order the List with a custom Comparator , and print it: 使用自定义Comparator订购List ,然后打印:

     students.sort(new Comparator<Student>() { @Override public int compare(final Student s1, final Student s2) { return s1.getPrizeMoney()-s2.getPrizeMoney(); } }); for (final Student student: students) System.out.println(student); 

Output : 输出

Javis%4532%100
Kevin%7657%400
Green%3243%1000
Stefan%31231%1000
IronMan%5435%2000
Lamp%534%3000
Bob%5435%5000
Frank%345%10000

我认为在这里创建一个3d数组,数组中从右到左的8x8x8是row,col,并且因此[0] [0] [1]是块1或kevin [0] [1] [1]是7657 [1 ] [1] [1]是400.我喜欢这种方式,因为它不仅为每个“项目”提供了一个数组,它还可以使它保持井然有序且易于访问

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

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