2014-11-05 38 views
0

我想鏈接並按降序排列二維數組。我以某種方式編寫我的程序,以便您可以打印數組,但它未被排序。我怎樣才能把它分類?如何以降序顯示二維數組

這是一個程序來計算的8名員工工作小時數在一週(7天),並打印出來按降序排列:

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 0 : 34.0 
Employee 1 : 28.0 
Employee 2 : 20.0 
Employee 3 : 31.0 
Employee 4 : 32.0 
Employee 5 : 28.0 
Employee 6 : 37.0 
Employee 7 : 41.0 

但我應該得到的東西是這樣的:

Employee 7: 41 
Employee 6: 37 
Employee 0: 34 
Employee 4: 32 
Employee 3: 31 
Employee 1: 28 
Employee 5: 28 
Employee 2: 20 
+0

您是否想按_total_工作時間對員工進行排序? – APerson 2014-11-05 02:43:20

+0

這應該回答你的問題http://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array 在比較函數中,使用數組的總和。 – yts 2014-11-05 02:51:02

回答

0

嘗試Arrays.sort()方法,它的自定義比較(來源:this answer):

java.util.Arrays.sort(employeeWorkHours, new java.util.Comparator<double[]>() { 
    public int compare(double[] a, double[] b) { 
     return Double.compare(a[0], b[0]); 
    } 
}); 

如果你不喜歡使用Java API,這裏有一個二維數組selection sort

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; 
} 
0

我建議你開始用EmployeePOJO存儲工作的時間和員工編號一樣

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); 
    } 
} 

然後你ç一個實現你sumRowfor-each和類似

public static double sumRow(double[] m) { 
    double total = 0; 
    for (double val : m) { 
     total += val; 
    } 
    return total; 
} 

最後,我想你main()可以再使用Arrays.sort(Object[])

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 
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; 
}