【问题标题】:How to apply Selection method to sort in alphabetical order?如何应用选择方法按字母顺序排序?
【发布时间】:2020-09-11 15:53:29
【问题描述】:
Estudiante[] aux = new Estudiante[1];
int contador = 0;
int contador2 = 1;
for (int i = 1; i < arregloEstudiante.length - 1; i++) {
    String minimo = arregloEstudiante[contador].getCarnet();
    int menor = i;
    for (int j = i + 1; j < arregloEstudiante.length; j++) {
        String maximo = arregloEstudiante[contador2].getCarnet();
        if (minimo.compareTo(maximo) < 0) {
            menor = j;
        }
        if (menor == j) {
            aux[0] = arregloEstudiante[i];
            arregloEstudiante[i] = arregloEstudiante[menor];
            arregloEstudiante[menor] = aux[0];
        }
    }
    contador++;
    contador2++;
}
return arregloEstudiante;

这是我最接近的结果。

C54411
B92542
A95720
A22523
B4562
B32567
B42667
C72588
C42214
C34767

我们必须使用选择方法。使用这个数组

        arregloEstudiante[0] = new Estudiante("C54411", "Maria", "Mora Mora", 700);
        arregloEstudiante[1] = new Estudiante("B92542", "Jose", "Solano Solano", 444);
        arregloEstudiante[2] = new Estudiante("C42214", "Alonso", "Solano Mora", 800);
        arregloEstudiante[3] = new Estudiante("A95720", "Miguel", "Mora Solano", 550);
        arregloEstudiante[4] = new Estudiante("B32567", "Andrea", "Jimenez Ureña", 625);
        arregloEstudiante[5] = new Estudiante("C34767", "Fabian", "Sanchez Alvarado", 740);
        arregloEstudiante[6] = new Estudiante("C72588", "Martin", "Moya Ureña", 592);
        arregloEstudiante[7] = new Estudiante("B42667", "Fabiana", "Sanchez Alvarado", 689);
        arregloEstudiante[8] = new Estudiante("A22523", "Mariano", "Mora Mora", 750);
        arregloEstudiante[9] = new Estudiante("B4562", "Alonso", "Solano Morales", 497);

仅使用第一个属性,我将它们保存在 temp、“minimo”和“maximo”中` 这是我最接近的结果。

C54411
B92542
A95720
A22523
B4562
B32567
B42667
C72588
C42214
C34767

我必须使用选择顺序方法。使用这个数组,我必须按字母顺序,第一个属性,我将它们保存在临时“minimo”和“maximo”中。

我使用这个类。

public class Estudiante {

private String carnet;
private String nombre;
private String apellidos;
private int notaAdmision;

public Estudiante() {

}

public Estudiante(String carnet, String nombre, String apellidos, int notaAdmision) {
    this.carnet = carnet;
    this.nombre = nombre;
    this.apellidos = apellidos;
    this.notaAdmision = notaAdmision;
}

public String getCarnet() {
    return this.carnet;
}

public String getNombre() {
    return this.nombre;
}

public String getApellidos() {
    return this.apellidos;
}

public int getNotaAdmision() {
    return this.notaAdmision;
}

public void setCarnet(String carnet) {
    this.carnet = carnet;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public void setApellidos(String apellidos) {
    this.apellidos = apellidos;
}

public void setNotaAdmision(int notaAdmision) {
    this.notaAdmision = notaAdmision;
}

这是我的课,我认为一切都很好。 我必须使用选择顺序方法。 如果有人找到解决方案,请告诉我。 用 JAVA 1.8 编译

【问题讨论】:

  • 欢迎来到 Stack Overflow。不幸的是,你似乎忘了问一个问题,或者至少你没有把它当作一个问题,所以请阅读How to Ask。事实上,您声称“一切正常”,如果这是真的,则意味着您没有真正的问题,因为您的代码有效。如果是这样的话,恭喜你。但是,如果并非一切正常,您应该告诉我们什么不工作:您使用什么输入,您期望什么输出,以及您实际得到什么输出?

标签: java arrays java-8


【解决方案1】:

您正在使用selection sort 根据carnet 属性对Estudiante 的数组进行排序。

我从this 网页上复制了代码并对其进行了修改以满足您的需求。

import java.util.Arrays;

public class Estudiante {
    private String carnet;
    private String nombre;
    private String apellidos;
    private int notaAdmision;

    public Estudiante(String carnet, String nombre, String apellidos, int notaAdmision) {
        this.carnet = carnet;
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.notaAdmision = notaAdmision;
    }

    public String getCarnet() {
        return this.carnet;
    }

    public String getNombre() {
        return this.nombre;
    }

    public String getApellidos() {
        return this.apellidos;
    }

    public int getNotaAdmision() {
        return this.notaAdmision;
    }

    public void setCarnet(String carnet) {
        this.carnet = carnet;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    public void setNotaAdmision(int notaAdmision) {
        this.notaAdmision = notaAdmision;
    }

    public static void sortAscending(final Estudiante[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minElementIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minElementIndex].getCarnet().compareTo(arr[j].getCarnet()) > 0) {
                    minElementIndex = j;
                }
            }
            if (minElementIndex != i) {
                Estudiante temp = arr[i];
                arr[i] = arr[minElementIndex];
                arr[minElementIndex] = temp;
            }
        }
    }

    public static void main(String[] args) {
        Estudiante[] arregloEstudiante = new Estudiante[10];
        arregloEstudiante[0] = new Estudiante("C54411", "Maria", "Mora Mora", 700);
        arregloEstudiante[1] = new Estudiante("B92542", "Jose", "Solano Solano", 444);
        arregloEstudiante[2] = new Estudiante("C42214", "Alonso", "Solano Mora", 800);
        arregloEstudiante[3] = new Estudiante("A95720", "Miguel", "Mora Solano", 550);
        arregloEstudiante[4] = new Estudiante("B32567", "Andrea", "Jimenez Ureña", 625);
        arregloEstudiante[5] = new Estudiante("C34767", "Fabian", "Sanchez Alvarado", 740);
        arregloEstudiante[6] = new Estudiante("C72588", "Martin", "Moya Ureña", 592);
        arregloEstudiante[7] = new Estudiante("B42667", "Fabiana", "Sanchez Alvarado", 689);
        arregloEstudiante[8] = new Estudiante("A22523", "Mariano", "Mora Mora", 750);
        arregloEstudiante[9] = new Estudiante("B4562", "Alonso", "Solano Morales", 497);
        sortAscending(arregloEstudiante);
        Arrays.stream(arregloEstudiante)
              .forEach(e -> System.out.println(e.getCarnet()));
    }
}
  • 我添加的唯一方法是sortAscending()
  • 代码的最后几行简单地打印出排序后的数组。它们不影响实际的排序操作。您可以使用任何您喜欢的方法打印出排序后的数组 - 如果您需要打印它。

下面是我运行上述代码时得到的输出。

A22523
A95720
B32567
B42667
B4562
B92542
C34767
C42214
C54411
C72588

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多