Java 如何按降序显示二维数组
我想按降序链接和排序二维数组。我以某种方式对程序进行了编码,以便您可以打印数组,但它没有排序。我怎样才能把它分类 这是一个程序,用于计算8名员工在一周7天内的工作小时数,并按降序打印出来:Java 如何按降序显示二维数组,java,sorting,multidimensional-array,Java,Sorting,Multidimensional Array,我想按降序链接和排序二维数组。我以某种方式对程序进行了编码,以便您可以打印数组,但它没有排序。我怎样才能把它分类 这是一个程序,用于计算8名员工在一周7天内的工作小时数,并按降序打印出来: public class WeeklyHours { public static void main(String[] args) { double[][] employeeWorkHours = { { 2, 4, 3, 4, 5, 8, 8 },
public class WeeklyHours {
public static void main(String[] args) {
double[][] employeeWorkHours = {
{ 2, 4, 3, 4, 5, 8, 8 },
{ 7, 3, 4, 3, 3, 4, 4 },
{ 3, 3, 4, 3, 3, 2, 2 },
{ 9, 3, 4, 7, 3, 4, 1 },
{ 3, 5, 4, 3, 6, 3, 8 },
{ 3, 4, 4, 6, 3, 4, 4 },
{ 3, 7, 4, 8, 3, 8, 4 },
{ 6, 3, 5, 9, 2, 7, 9 } };
for (int row = 0; row < employeeWorkHours.length; row++)
System.out.println("Employee " + row + " : "
+ sumRow(employeeWorkHours, row));
}
public static double sumRow(double[][] m, int rowIndex) {
double total = 0;
for (int col = 0; col < m[0].length; col++) {
total += m[rowIndex][col];
}
return total;
}
}
但我应该得到这样的东西:
Employee 7: 41
Employee 6: 37
Employee 0: 34
Employee 4: 32
Employee 3: 31
Employee 1: 28
Employee 5: 28
Employee 2: 20
尝试采用自定义比较器源的方法::
如果您不想使用Java API,请在二维数组上执行以下操作:
int numEmployees = employeeWorkHours.length; // for convenience
// Looping through each of the employees
for(int employee = 0; employee < numEmployees; employee++) {
// Find the employee who's worked the most out of the remaining ones
int maxTimeEmployee = employee; // Start with the current one
for(int i = employee; i < numEmployees; i++) {
if(sumRow(employeeWorkHours, i) > sumRow(employeeWorkHours, maxTimeEmployee)) {
// We've found a new maximum
maxTimeEmployee = i;
}
}
// Swap the current employee with the maximum one
double[] tempHours = employeeWorkHours[employee];
employeeWorkHours[employee] = employeeWorkHours[maxTimeEmployee];
employeeWorkHours[maxTimeEmployee] = tempHours;
}
我建议您从一名员工开始存储每周工作时间和员工编号,如
static class Employee implements Comparable<Employee> {
int num;
double hours;
public Employee(int num, double hours) {
this.num = num;
this.hours = hours;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
@Override
public String toString() {
return String.format("Employee #%d - Hours %.1f", num, hours);
}
@Override
public int compareTo(Employee o) {
int c = Double.valueOf(this.hours).compareTo(o.hours);
if (c != 0) {
return -c;
}
return Integer.valueOf(this.num).compareTo(o.num);
}
}
最后,我想你的主菜单可以用
对代码进行一些更改:
public class WeeklyHours {
public static void main(String[] args) {
double[][] employeeWorkHours = {
{ 2, 4, 3, 4, 5, 8, 8 },
{ 7, 3, 4, 3, 3, 4, 4 },
{ 3, 3, 4, 3, 3, 2, 2 },
{ 9, 3, 4, 7, 3, 4, 1 },
{ 3, 5, 4, 3, 6, 3, 8 },
{ 3, 4, 4, 6, 3, 4, 4 },
{ 3, 7, 4, 8, 3, 8, 4 },
{ 6, 3, 5, 9, 2, 7, 9 } };
int len=employeeWorkHours.length;
double[][] employeeTotal=new double [len][2];
for (int row = 0; row < len; row++) {
employeeTotal[row][0]=row;
employeeTotal[row][1]=sumRow(employeeWorkHours, row);
System.out.println("Employee " + (int)employeeTotal[row][0] +
" : " + employeeTotal[row][1]);
}
System.out.println("\nOrder by Hours:");
Arrays.sort(employeeTotal, new java.util.Comparator<double[]>() {
public int compare(double[] a, double[] b) {
return Double.compare(b[1], a[1]);
}
});
for (int row = 0; row < len; row++)
System.out.println("Employee " + (int) employeeTotal[row][0] +
" : " + employeeTotal[row][1]);
}
public static double sumRow(double[][] m, int rowIndex) {
double total = 0;
for (int col = 0; col < m[0].length; col++) {
total += m[rowIndex][col];
}
return total;
}
是否要按总工作时间对员工进行排序?这应该可以回答您在比较函数中的问题,使用数组的总和。
static class Employee implements Comparable<Employee> {
int num;
double hours;
public Employee(int num, double hours) {
this.num = num;
this.hours = hours;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
@Override
public String toString() {
return String.format("Employee #%d - Hours %.1f", num, hours);
}
@Override
public int compareTo(Employee o) {
int c = Double.valueOf(this.hours).compareTo(o.hours);
if (c != 0) {
return -c;
}
return Integer.valueOf(this.num).compareTo(o.num);
}
}
public static double sumRow(double[] m) {
double total = 0;
for (double val : m) {
total += val;
}
return total;
}
public static void main(String[] args) {
double[][] employeeWorkHours = { { 2, 4, 3, 4, 5, 8, 8 },
{ 7, 3, 4, 3, 3, 4, 4 }, { 3, 3, 4, 3, 3, 2, 2 },
{ 9, 3, 4, 7, 3, 4, 1 }, { 3, 5, 4, 3, 6, 3, 8 },
{ 3, 4, 4, 6, 3, 4, 4 }, { 3, 7, 4, 8, 3, 8, 4 },
{ 6, 3, 5, 9, 2, 7, 9 } };
Employee[] totals = new Employee[employeeWorkHours.length];
for (int i = 0; i < employeeWorkHours.length; i++) {
totals[i] = new Employee(i, sumRow(employeeWorkHours[i]));
}
Arrays.sort(totals);
for (Employee e : totals) {
System.out.println(e);
}
}
Employee #7 - Hours 41.0
Employee #6 - Hours 37.0
Employee #0 - Hours 34.0
Employee #4 - Hours 32.0
Employee #3 - Hours 31.0
Employee #1 - Hours 28.0
Employee #5 - Hours 28.0
Employee #2 - Hours 20.0
public class WeeklyHours {
public static void main(String[] args) {
double[][] employeeWorkHours = {
{ 2, 4, 3, 4, 5, 8, 8 },
{ 7, 3, 4, 3, 3, 4, 4 },
{ 3, 3, 4, 3, 3, 2, 2 },
{ 9, 3, 4, 7, 3, 4, 1 },
{ 3, 5, 4, 3, 6, 3, 8 },
{ 3, 4, 4, 6, 3, 4, 4 },
{ 3, 7, 4, 8, 3, 8, 4 },
{ 6, 3, 5, 9, 2, 7, 9 } };
int len=employeeWorkHours.length;
double[][] employeeTotal=new double [len][2];
for (int row = 0; row < len; row++) {
employeeTotal[row][0]=row;
employeeTotal[row][1]=sumRow(employeeWorkHours, row);
System.out.println("Employee " + (int)employeeTotal[row][0] +
" : " + employeeTotal[row][1]);
}
System.out.println("\nOrder by Hours:");
Arrays.sort(employeeTotal, new java.util.Comparator<double[]>() {
public int compare(double[] a, double[] b) {
return Double.compare(b[1], a[1]);
}
});
for (int row = 0; row < len; row++)
System.out.println("Employee " + (int) employeeTotal[row][0] +
" : " + employeeTotal[row][1]);
}
public static double sumRow(double[][] m, int rowIndex) {
double total = 0;
for (int col = 0; col < m[0].length; col++) {
total += m[rowIndex][col];
}
return total;
}