对教师的二维数组进行排序

时间:2014-03-31 15:46:19

标签: java arrays sorting multidimensional-array

我需要用一个方法来做一个程序,该方法接收一个二维3 * 3数组,其中包含几位教授的信息(在名为&#34的类中创建; Profesor"。这些信息包括他们的ID,名称,姓氏,年龄,性别,工作小时数以及每小时赢得多少,按照建设者设定的特定顺序),并按照年龄和日薪分两种方式对其进行排序。他们的信息是通过构造函数提供的。此外,他们的日薪是使用Salary()方法(在" Profesor"类中)计算的,该方法返回他们每小时的金额乘以小时数。 到目前为止,这是我的代码:

public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner in = new Scanner(System.in);

    Profesor arreglo[][];
    arreglo = new Profesor[3][3];

    Profesor profe1 = new Profesor(126509404, "Edgar", "Hernandez", 28, "M", 8, 0);
    profe1.setMoneyPerHour(1.66);
    Profesor profe2 = new Profesor(233849920, "Nuria", "Ramírez", 52, "F", 4, 0);
    profe2.setMoneyPerHour(2.43);
    Profesor profe3 = new Profesor(126509404, "José", "Hernandez", 29, "M", 8, 0);
    profe3.setMoneyPerHour(1.84);
    Profesor profe4 = new Profesor(126509404, "Arlene", "Hernandez", 34, "F", 6, 0);
    profe4.setMoneyPerHour(2.00);
    Profesor profe5 = new Profesor(126509404, "Orlando", "Hernandez", 36, "M", 10, 0);
    profe5.setMoneyPerHour(1.66);
    Profesor profe6 = new Profesor(126509404, "Ximena", "Hernandez", 55, "F", 4, 0);
    profe6.setMoneyPerHour(2.30);
    Profesor profe7 = new Profesor(126509404, "Eduardo", "Hernandez", 44, "M", 8, 0);
    profe7.setMoneyPerHour(1.66);
    Profesor profe8 = new Profesor(126509404, "Johanna", "Hernandez", 35, "F", 8, 0);
    profe8.setMoneyPerHour(1.66);
    Profesor profe9 = new Profesor(126509404, "Gilberto", "Hernandez", 42, "M", 8, 0);
    profe9.setMoneyPerHour(1.66);


    arreglo[0][0] = profe1;
    arreglo[0][1] = profe2;
    arreglo[0][2] = profe3;

    arreglo[1][0] = profe4;
    arreglo[1][1] = profe5;
    arreglo[1][2] = profe6;

    arreglo[2][0] = profe7;
    arreglo[2][1] = profe8;
    arreglo[2][2] = profe9;

计算工资的方法如下:

public double salary(){
    double salary = hours*moneyPerHour;
    return salary;
}

根据我们在课堂上所教授的内容,这是我迄今为止对阵列进行排序的尝试:

public static void ordenaProfes(Profesor matrix[][]) {
  int ix = 0;
  int jx = 0;
  double minorSalary= matriz[0][0].salary();
  int n = matrix.length;
  for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
          double aux= matrix[i][i].salary();
          if(aux  < minorSalary){
              ix = i;
              jx = j;
              minorSalary = aux;
          }
      }
  }

但是使用它不会起作用。它没有对数组进行排序,只返回arreglo [0] [0]的工资。

1 个答案:

答案 0 :(得分:2)

好的,这是冒泡排序:

public static void ordenaProfes(Profesor matrix[][]) {
  int n = matrix.length;
  boolean changed = true; //need to check, if something have changed
  while(changed){
      changed = false; //set changed to false at the begining
      int ix = 0;
      int jx = 0;
      double lastSalary= matrix[0][0].salary(); //
      for(int i = 0; i < n; i++){
          for(int j = 0; j < n; j++){
              double aux= matrix[i][j].salary();
              if(aux  < lastSalary){
                  changed = true; //set change to true, so you know, that you have to iterate 1 more timr
                  //swap the element
                  Professor temp = matrix[ix][jx];
                  matrix[ix][jx] = matrix[i][j];
                  matrix[i][j] = temp;
              }
              //save the last element
              ix = i;
              jx = j;
              lastSalary = aux;
          }
      }
  }
好吧,试一下,我没有测试代码,所以我希望我没有忘记一些事情

编辑:交换改进

EDIT2:将变量初始化移动到while循环