Java - Tres mejores tiempos

 
Vista:
Imágen de perfil de Devastador
Val: 47
Ha aumentado su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

Tres mejores tiempos

Publicado por Devastador (15 intervenciones) el 30/10/2020 20:40:48
Hola, tengo una duda, tengo que hacer un botón en java que me imprima los 3 mejores tiempos ingresado en un arreglo, el problema es que el arreglo no tiene limite y por eso se debe comparar según el tamaño del arreglo y es allí donde no se que hacer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private void winnermActionPerformed(java.awt.event.ActionEvent evt) {
    for (int i=0; i < array.length; i++){
        if (array[i].getTiempocarrera() > 0){
            if (array[i].getSexo().equals("Femenino")){
                modelo.insertRow(contador3,new Object[]{});
                modelo.setValueAt(array[i].getNumcorredor(),contador3, 0);
                modelo.setValueAt(array[i].getNombre(), contador3, 1);
                modelo.setValueAt(array[i].getApellido(), contador3, 2);
                modelo.setValueAt(array[i].getTiempocarrera(), contador3, 3);
                modelo.setValueAt(array[i].getFacultad(), contador3, 4);
 
            }
        }
        else {
            JOptionPane.showMessageDialog(rootPane, "No se ha ingresado ningún tiempo","Alerta",JOptionPane.WARNING_MESSAGE);
        }
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Tres mejores tiempos

Publicado por Billy Joel (876 intervenciones) el 02/11/2020 22:38:54
He tratado de replicar tu código para dar una solución y ésta no es mas que ordenamiento.
Primero te voy a dejar el método de ordenamiento por burbuja que lleva acompañando por mucho tiempo XD
1
2
3
4
5
6
7
8
9
10
11
public static void ordenamientoBurbuja(Participante[] array) {
    for (int i = array.length - 1; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            if (j + 1 <= i && array[j].getTiempocarrera() < array[j + 1].getTiempocarrera()) {
                Participante aux = array[j];
                array[j] = array[j + 1];
                array[j + 1] = aux;
            }
        }
    }
}

No se de que tiepo es tu array yo me cree una clase Participante con las propiedades que has descrito y bueno ahí va,
Entonces la idea es la siguiente,
- Obtener una lista de participantes por sexo
- Se ordena la lista de menor tiempo a mayor tiempo.
- Se devuelve el top 3 que serían los mejores tiempos.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public Participante[] getMejoresTiemposFemeninos(Participante[] array) {
    List<Participante> f = new ArrayList();
    for (Participante p : array) {
        if (p.getSexo().equalsIgnoreCase("Femenino")) {
            f.add(p);
        }
    }
 
    if (f.isEmpty()) {
        return null;
    } else {
        Participante[] femenino = new Participante[f.size()];
        f.toArray(femenino);
        ordenamientoBurbuja(femenino);
        int size;
        if (femenino.length < 3) {
            size = femenino.length;
        } else {
            size = 3;
        }
        Participante[] top = new Participante[size];
        for (int i = 0; i < top.length; i++) {
            top[i] = femenino[i];
        }
        return top;
    }
}

Cualquier duda solo escribe.
Saludos,
Billy Joel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Devastador
Val: 47
Ha aumentado su posición en 2 puestos en Java (en relación al último mes)
Gráfica de Java

Tres mejores tiempos

Publicado por Devastador (15 intervenciones) el 04/11/2020 00:52:33
Hola despues de hacer la pregunta empece a trabajar mi cuenta y pude hacer que me imprimiera el primer y segundo lugar, pero cuando imprime el tercer mejor tiempo me imprime el tiempo más grande y no el tercer menor tiempo, obviamente hice todo esto antes de ver tu respuesta

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class winner extends javax.swing.JInternalFrame {
    float segundo;
    float tercero = 0;
    float primero = 1000;
    private final inscritos [] array;
    public winner(inscritos []arrayInscritos) {
        array = arrayInscritos;
        initComponents();;
    }
 
    private void mostrarganadorasActionPerformed(java.awt.event.ActionEvent evt) {
         for (int i=0; i < array.length; i++){
            if (array[i].getTiempocarrera() > 0){
                if (array[i].getSexo().equals("Femenino")){
                    if(array[i].getTiempocarrera() < primero){
                        primero = array[i].getTiempocarrera();
                        num1.setText("No. Corredora "+Integer.toString(array[i].getNumcorredor()));
                        nombre1.setText(array[i].getNombre());
                        apellido1.setText(array[i].getApellido());
                        tiempo1.setText("Tiempo "+Float.toString(primero)+" minutos");
                        facultad1.setText(array[i].getFacultad());
                    }
                    if(array[i].getTiempocarrera() < tercero && array[i].getTiempocarrera() > primero){
                        segundo = array[i].getTiempocarrera();
                        num2.setText("No. Corredora "+Integer.toString(array[i].getNumcorredor()));
                        nombre2.setText(array[i].getNombre());
                        apellido2.setText(array[i].getApellido());
                        tiempo2.setText("Tiempo "+Float.toString(segundo)+" minutos");
                        facultad2.setText(array[i].getFacultad());
 
                    }
                    if(array[i].getTiempocarrera() > tercero){
                        tercero = array[i].getTiempocarrera();
                        num3.setText("No. Corredora "+Integer.toString(array[i].getNumcorredor()));
                        nombre3.setText(array[i].getNombre());
                        apellido3.setText(array[i].getApellido());
                        tiempo3.setText("Tiempo "+Float.toString(tercero)+" minutos");
                        facultad3.setText(array[i].getFacultad());
                    }
                }
            }
            else if (array[i].getTiempocarrera() == 0) {
                JOptionPane.showMessageDialog(rootPane, "No se ha ingresado ningún tiempo","Alerta",JOptionPane.WARNING_MESSAGE);
            }
        }
    }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar