【问题标题】:Sorting Arraylist Java对数组列表进行排序 Java
【发布时间】:2013-11-10 18:29:21
【问题描述】:

我有一个放入数组的 txt 文件。 txt文件中数据的格式为:

Order #     Date     Name     City     State    Zip Code    Transaction Amount

跨行,每个项目代表一列。

然后还有 1000 行以上填写了答案。我想按交易金额排序。

这是我的代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Rewards {

public static void main(String[] args) throws FileNotFoundException {

    String fileName = ("test.txt");
    FileReader fin = new FileReader("C:/Users/Jordan/Desktop/Project5Text.txt");
    Scanner src = new Scanner(fin);
    ArrayList<String> lines = new ArrayList<String>();
    src.useDelimiter(":");

    Random rand = new Random();

    while (src.hasNext()) {
        String l = src.nextLine();
        if (!l.equals(""))
            lines.add(l);
    }
    String[] randomChoices = new String[1]; // Number of random choices

    for (int i = 0; i < randomChoices.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices[i] = randomString;
    }

    for (String s : randomChoices)
        System.out.println("Random Winner for $20 gift card is:       " +s);

    String[] randomChoices1 = new String[1]; // Number of random choices

    for (int i = 0; i < randomChoices1.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices1[i] = randomString;
    }

    for (String s : randomChoices1)
        System.out.println("Random Winner for $40 gift card is:       " +s);

    String[] randomChoices2 = new String[1]; // Number of random choices

    for (int i = 0; i < randomChoices2.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices2[i] = randomString;
    }

    for (String s : randomChoices2)
        System.out.println("Random Winner for $60 gift card is:       " +s);

    String[] randomChoices3 = new String[1]; // Number of random choices

    for (int i = 0; i < randomChoices3.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices3[i] = randomString;
    }

    for (String s : randomChoices3)
        System.out.println("Random Winner for $80 gift card is:       " +s);

    String[] randomChoices4 = new String[1]; // Number of random choices

    for (int i = 0; i < randomChoices4.length; i++) {
        String randomString = lines.get(1 + rand.nextInt(lines.size() - 1));
        randomChoices4[i] = randomString;
    }

    for (String s : randomChoices4)
        System.out.println("Random Winner for $100 gift card is:      " +s);

}
}

这是我正在使用的一些文本文件。

订单编号 日期 名字 中间名 首字母 姓氏 地址 城市 州 邮编 电子邮件 交易金额

1 2012 年 8 月 26 日 Kristina H Chung 947 Martin Ave. Muncie CA 46489 khchung@business.com $593

2 2012 年 11 月 16 日 Paige H Chen 15 MainWay Rd.达拉斯 HI 47281 phchen@business.com $516

3 2012 年 11 月 10 日 Sherri E Melton 808 Washington Way Brazil CA 47880 semelton@business.com $80

4 2012 年 9 月 20 日 Gretchen I Hill 56 Washington Dr. Atlanta FL 47215 gihill@business.com 989 美元

5 2012 年 3 月 11 日 Karen U Puckett 652 Maplewood Ct.巴西佛罗里达州 46627 kupuckett@business.com $826

6 2012 年 7 月 4 日 Patrick O Song 679 MainWay Rd.老佛爷 GA 47161 posong@business.com $652

【问题讨论】:

    标签: sorting arraylist


    【解决方案1】:

    这样做的一种方法是创建一个可以表示文件中的一行的类。在本课程中,您将拥有每一列的字段。让这个类实现 Comparable 接口。这迫使您实现 compareTo() 方法。

    完成此操作后,您可以简单地使用此类创建一个 ArrayList 并使用内置的 sort() 方法对其进行排序!

    【讨论】:

      【解决方案2】:

      我建议您首先创建一个以逻辑方式存储行的类:

      class Transaction{
          String date;
          String name;
          String address;
          int amount;
          // etc...
      }
      

      然后你可以将每个事务存储在一个 List 中,并编写一个自定义的 Comparator:

      List<Transaction> transactions = new ArrayList<Transaction>();
      ...
      Collections.sort(transactions, new Comparator<Transaction>(){
          public int compare(Transaction t1, Transaction t2){
              return Double.compare(t1.amount, t2.amount);
          }
      });
      

      【讨论】:

        【解决方案3】:

        如果您需要一个简单(但不是最高效)的解决方案,您可以这样做:

            lines.sort(new Comparator<String>() {
        
                public double dollar(String line) {
                    String[] parts = line.split(" ");
                    // take the last element and remove its first character ($)
                    return Double.parseDouble(parts[parts.length - 1].substring(1));
                }
                @Override
                public int compare(String o1, String o2) {
                    // swap o1 and o2 if you need to sort in the opposite order
                    return Double.compare(dollar(o1), dollar(o2));
                }
            });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-09
          • 2013-07-09
          • 2019-06-09
          • 2017-12-13
          • 1970-01-01
          相关资源
          最近更新 更多