Java Eclipse排序然后排序1个数组,也许更多?

时间:2016-03-27 22:16:45

标签: java arrays sorting

以下是一个Java分配问题,我已经完成了大部分工作,但是我遇到了以降序显示输出的问题(从最高工作小时数到最低工作小时数,同时也将这些时间与工作的员工相匹配)。我在粗体中突出了我需要帮助的部分问题,并包含了示例输出。

问题:

计算员工的每周工作时间假设所有员工的每周工作时间都存储在二维数组中。每行记录员工的n天工作时间,其中n列为n≥1,n≤7表示这些员工每周工作的天数。例如,下面的表格表示一个阵列,用于存储一周内7天内8名员工的工作时间。编写一个程序,作为输入,员工人数和一周内的工作日数。然后 它接收所有员工信息(每日工作时间的名称和数量)。 此计划应按照总小时数的递减顺序显示员工及其一周工作总时数。

示例输出:

Employee 7 worked 39 hours

Employee 6 worked 37 hours

Employee 0 worked 34 hours

Employee 4 worked 32 hours

Employee 3 worked 31 hours

Employee 1 worked 28 hours

Employee 5 worked 28 hours

Employee 2 worked 24 hours

这是我到目前为止所得到的,我相信我在最后for循环中遗漏了一些东西,但这就是我发布这个的原因。非常感谢任何帮助:

import java.util.Scanner;
import java.util.Arrays;

public class EmployeeWorkHours {
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);
      System.out.println("How many Employee's do you have?: ");
      int NUM_OF_EMPLOYEES = scan.nextInt();
      scan.nextLine();

      int [][]hours;
      int []totalHours= new int[NUM_OF_EMPLOYEES];

      hours = new int[NUM_OF_EMPLOYEES][7];
      String[] employee = new String[NUM_OF_EMPLOYEES];

      // input Names
      for (int x = 0; x < (employee.length); x++) 
      {
          System.out.println("Name of Employee " + (x + 1) + ": ");
          String name = scan.nextLine();
          employee[x] = name;

      }

      // input Hours
      for (int z = 0; z < employee.length; z++) 
      {
          System.out.println("Beginning on Monday, enter the hours Employee "+ (z + 1)+ " has worked each day (Separate each day by spaces): ");
          for (int a = 0; a < 7; a++) 
          {
              hours[z][a] = scan.nextInt();
          }
          scan.nextLine();
      }

      // Print everything in for loop
      for (int i = 0; i < employee.length; i++) 
      {
          totalHours[i]=0;
          for(int j=0; j<7; j ++)
          {
              totalHours[i] = totalHours[i]+hours[i][j];   
          }
          // I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
          // Arrays.sort(totalHours); gives me the wrong output, I'm stuck

          System.out.println("Employee " + (i + 1) +" worked " + totalHours[i] + " hours");  
      }
}
}

1 个答案:

答案 0 :(得分:1)

以下代码的快速但不良的性能补丁:

// Print everything in for loop
for (int i = 0; i < employee.length; i++) {
    totalHours[i] = 0;
    for (int j = 0; j < 7; j++) {
        totalHours[i] = totalHours[i] + hours[i][j];
    }
    // I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
    // Arrays.sort(totalHours); gives me the wrong output, I'm stuck

}

//patch starts here
int[] totalHoursSortedAsc = Arrays.copyOf(totalHours, totalHours.length);
Arrays.sort(totalHoursSortedAsc);
for (int i = totalHoursSortedAsc.length - 1;i>=0;i--) {
    for (int j = 0;j < totalHours.length;j++) {
        if (totalHoursSortedAsc[i] == totalHours[j]) {
            System.out.println("Employee " + (j + 1) + " worked " + totalHours[j] + " hours");
            totalHours[j] = -1; //Employees may work the same time
        }
    }
}

更好的方法是按照安德烈亚斯的建议,按EmployeeWorkHoursCollections.sort()进行排序。查看here示例代码。