Java 排序整数数组,同时保持与字符串数组的关系

Java 排序整数数组,同时保持与字符串数组的关系,java,arrays,sorting,arraylist,Java,Arrays,Sorting,Arraylist,我的程序模拟电梯(电梯)的运行,每次“用户”使用电梯时,它都会在文本文件中创建一行 文本文件示例: 25/03/14\u 05:09:24 Slccj AZEP1 2 25/03/14_05:37:48 Wfvp DVJHLWPC 3 5 25/03/14_05:38:27 Mfkk Wtrsejplc 2 1 2014年3月25日05:39:00 Qczoz Mlrrtyd 0 4 每次使用都会在文件中创建一行,其中包含时间戳、(编码的)用户名、他们进入电梯的楼层和下车的楼层 我一直在想如何打

我的程序模拟电梯(电梯)的运行,每次“用户”使用电梯时,它都会在文本文件中创建一行

文本文件示例:
25/03/14\u 05:09:24 Slccj AZEP1 2
25/03/14_05:37:48 Wfvp DVJHLWPC 3 5
25/03/14_05:38:27 Mfkk Wtrsejplc 2 1
2014年3月25日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");
}
public void showUseCount2(){
int[]userCount=新的int[4];
字符串[]用户=新字符串[4];
用户[0]=“Wfvp-Dvjhlwvpc”;
用户[1]=“Qczoz Mlrrtyd”;
用户[2]=“Slccj AZEPC”;
用户[3]=“Mfkk Wtrsejplc”;
试试{
弦斯特林;
for(int i=0;i
这是我目前得到的输出:
[[I@19b04e2]
Wfvp Dvjhlwvpc使用电梯:1次
Qczoz Mlrrtyd使用升降机:1次
Slccj Azeepc使用电梯:1次
Mfkk Wtrsejplc使用电梯:2次


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

O对象化日志记录,并解析日志行以创建对象

class LiftLogRecord {
   private final long userId;
   private final floor;
   private final numberOfTimes;
   private Date dateUsed;
   //rest of the code
}
然后使用write自定义比较器根据所需字段对其进行排序

Collections.sort(logRecordList, (LiftLogRecord r1, LiftLogRecord r2) -> r1.getNumberUsed()-r2.getNumberUsed()));

将日志记录对象化,并解析日志行以创建对象

class LiftLogRecord {
   private final long userId;
   private final floor;
   private final numberOfTimes;
   private Date dateUsed;
   //rest of the code
}
然后使用write自定义比较器根据所需字段对其进行排序

Collections.sort(logRecordList, (LiftLogRecord r1, LiftLogRecord r2) -> r1.getNumberUsed()-r2.getNumberUsed()));

将日志记录对象化,并解析日志行以创建对象

class LiftLogRecord {
   private final long userId;
   private final floor;
   private final numberOfTimes;
   private Date dateUsed;
   //rest of the code
}
然后使用write自定义比较器根据所需字段对其进行排序

Collections.sort(logRecordList, (LiftLogRecord r1, LiftLogRecord r2) -> r1.getNumberUsed()-r2.getNumberUsed()));

将日志记录对象化,并解析日志行以创建对象

class LiftLogRecord {
   private final long userId;
   private final floor;
   private final numberOfTimes;
   private Date dateUsed;
   //rest of the code
}
然后使用write自定义比较器根据所需字段对其进行排序

Collections.sort(logRecordList, (LiftLogRecord r1, LiftLogRecord r2) -> r1.getNumberUsed()-r2.getNumberUsed()));

您不需要对
int
s数组进行排序,而是需要定义一个类,该类保存您正在排序的值以及与之相关联的其他信息。然后,您将对这些对象的数组进行排序,而不是对
int
s数组进行排序。您可以使该类实现
Compariable
,也可以定义
比较器
作为第二个参数传递给
数组。排序
。示例:

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);
    }
}
类用户实现可比较的{
私人整数计数;
公共用户(字符串名称){
this.name=名称;
计数=0;
}
公共无效增量计数(){
计数++;
}
@凌驾
公共整数比较(用户其他){
返回整数.compare(count,other.count);
//或者,如果希望compareTo返回相反的结果,以便进行排序
//按降序排列:
//返回整数.compare(other.count,count);
}
}

您不需要对
int
s数组进行排序,而是需要定义一个类,该类保存您要排序的值以及与之相关联的其他信息。然后,您将对这些对象的数组进行排序,而不是对
int
s数组进行排序。您可以使该类实现
可比较的
,也可以定义一个
比较器
,作为第二个参数传递给
数组。排序
。示例:

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);
    }
}
类用户实现可比较的{
私人整数计数;
公共用户(字符串名称){
this.name=名称;
计数=0;
}
公共无效增量计数(){
计数++;
}
@凌驾
公共整数比较(用户其他){
返回整数.compare(count,other.count);
//或者,如果希望compareTo返回相反的结果,以便进行排序
//按降序排列:
//返回整数.compare(other.count,count);
}
}

您不需要对
int
s数组进行排序,而是需要定义一个类,该类保存您要排序的值以及与之相关联的其他信息。然后,您将对这些对象的数组进行排序,而不是对
int
s数组进行排序。您可以使该类实现
可比较的
,也可以定义一个
比较器
,作为第二个参数传递给
数组。排序
。示例:

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);
    }
}
类用户实现可比较的{
私人整数计数;
公共用户(字符串名称){
this.name=名称;
计数=0;
}
公共无效增量计数(){
计数++;
}
@凌驾
公共整数比较(用户o