排序一个int数组,同时保持与一个字符串数组的关系

时间:2014-03-25 20:15:09

标签: java arrays sorting arraylist

我的程序模拟升降机(升降机)的操作,每次用户使用时,它都会在文本文件中创建一条线。使用电梯。

文本文件示例:
25/03/14_05:09:24 Slccj Azeepc 1 2 25/03/14_05:37:48 Wfvp Dvjhlwvpc 3 5 25/03/14_05:38:27 Mfkk Wtrsejplc 2 1 25/03/14_05:39:00 Qczoz Mlrrtyd 0 4

每次使用都会在文件中创建一行,其中包含时间戳,(已编码)用户的姓名,他们进入电梯的楼层以及楼层已下车。

我一直试图找出如何打印报告,详细说明每位用户按降序使用升降机的次数。

这是我到目前为止所做的:

public void showUseCount2() {

    int[] userCount = new int[4];

    String[] users = new String[4];
    users[0] = "Wfvp Dvjhlwvpc";    
    users[1] = "Qczoz Mlrrtyd";     
    users[2] = "Slccj Azeepc";      
    users[3] = "Mfkk Wtrsejplc";    

    try {   
        String strLine;
        for (int i = 0; i < userCount.length; i++) {
            FileInputStream fis = new FileInputStream("reports.txt");
            DataInputStream dis = new DataInputStream(fis);
            BufferedReader br = new BufferedReader(new InputStreamReader(dis));
            while ((strLine = br.readLine()) != null) {
                int startIndex = strLine.indexOf(users[i]);
                while (startIndex != -1) {
                    userCount[i]++;
                    startIndex = strLine.indexOf(users[i], startIndex + users[i].length());
                }
            }
            dis.close();    
        }    
        Arrays.sort(userCount);
        System.out.println(Arrays.asList(userCount));
    }
    catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
    System.out.println("Wfvp Dvjhlwvpc used the lift: " + userCount[0] + " times");
    System.out.println("Qczoz Mlrrtyd used the lift: " + userCount[1] + " times");
    System.out.println("Slccj Azeepc used the lift: " + userCount[2] + " times");
    System.out.println("Mfkk Wtrsejplc used the lift: " + userCount[3] + " times");
}

这是我现在得到的输出:
[[I@19b04e2] Wfvp Dvjhlwvpc used the lift: 1 times Qczoz Mlrrtyd used the lift: 1 times Slccj Azeepc used the lift: 1 times Mfkk Wtrsejplc used the lift: 2 times

因此我的for循环成功填充了userCount数组。我尝试过使用Arrays.sort,但它似乎对我不起作用。因此,我需要一种方法来对阵列中的这些数字进行排序,同时保持用户与其使用计数之间的关系。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:2)

您需要定义一个类,该类包含您要使用与其关联的其他信息进行排序的值,而不是对int的数组进行排序。然后,您将对这些对象的数组进行排序,而不是int的数组。您可以使类实现Comparable,也可以将Comparator定义为Arrays.sort的第二个参数。例如:

class User implements Comparable<User> {
    private int count;
    public User (String name) {
        this.name = name;
        count = 0;
    }
    public void incrementCount() {
        count++;
    }
    @Override
    public int compareTo(User other) {
        return Integer.compare(count, other.count);
        // Or if you want compareTo to return the reverse result, so that you can sort
        // in descending order:
        // return Integer.compare(other.count, count);
    }
}

答案 1 :(得分:1)

对您的日志记录进行客观化,并解析日志行以创建对象

class LiftLogRecord {
   private final long userId;
   private final floor;
   private final numberOfTimes;
   private Date dateUsed;
   //rest of the code
}

然后使用写自定义比较器根据必填字段

对其进行排序
Collections.sort(logRecordList, (LiftLogRecord r1, LiftLogRecord r2) -> r1.getNumberUsed()-r2.getNumberUsed()));
相关问题