reorder array java

0

Good morning, I have two classes in java called Kart and another CarreraKart where I simulate a race. I would like to show in the array that I display the sorting already ordered when they move forward. The method has to be like that is the one that I can not get out of now

This is the main class CarreraKarts

package carrerakarts;


public class CarreraKarts {

public static void verClasificacion(int[] p) {

    int[] a = p;

    System.out.print("CLASIF.: (" + p[0]);
    for (int i = 1; i < p.length; i++) {
        System.out.print("," + a[i]);
    }
    System.out.print(" )\n");

}

public static int verPos(int[] p, int numero) {

    return 1;
}

public static void reordenar(int[] p, int numero, int adelanta) {
    for (int i = 0; i < p.length; i++) {
        // Recorrer desde la siguiente posición en adelante para comparar
        // el elemento en la posición conta con los de más adelante
        for (int j = i + 1; j < p.length; j++) {
            // Si el elemento inicial es mayor lo intercambiamos
            if (p[j] < p[i]) {
                adelanta = p[i];
                p[i] = p[j];
                p[j] = adelanta;
            }
        }
    }

}

public static void main(String[] args) {
    // ------- DECLARACIONES E INICIALIZACIONES
    // Declarar arrays de objetos
    Kart[] kart = new Kart[10];
    // Declarar array de posiciones
    // El coche nº 10 lo poenmos en posición 1, el 9 en la 2, ...
    int[] posicion = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    // Variable que registra adelantamientos
    int adelantamiento = 0;
    // Instanciar objetos
    for (int i = 0; i < 10; i++) {
        kart[i] = new Kart(i + 1, 40 + i);
    }
    // Ver propiedades de los objetos
    for (int i = 0; i < 10; i++) {
        System.out.println(kart[i]);
    }
    // --------------------------
    // Ver clasificación actual
    verClasificacion(posicion);
    // Dar una vuelta para todos los kart       
    for (int v = 0; v < 6; v++) {
        System.out.println("))))) VUELTA: " + v);
        for (int i = 0; i < 10; i++) {
            System.out.println("Inicio de la vuelta del coche " + kart[i].numero);
            adelantamiento = kart[i].vuelta();
            if ((adelantamiento != 0) && (verPos(posicion, kart[i].numero) - adelantamiento) >= 0) {
                reordenar(posicion, kart[i].numero, adelantamiento);
                System.out.println("El coche " + kart[i].numero + " tiene un premio por adelantar");
            }
        }
        // Ver clasificación actual
        verClasificacion(posicion);
    }
    // Ver propiedades de los objetos
    for (int i = 0; i < 10; i++) {
        System.out.println(kart[i]);
    }
}

}

I leave the Karts class.

package carrerakarts;


public class Kart {

int numero;
int deposito;
int velocidad;

public Kart(int numero, int velocidad) {
    this.numero = numero;
    this.velocidad = velocidad;
    this.deposito = 30;
}

public int getDeposito() {
    return deposito;
}

public int getVelocidad() {
    return velocidad;
}

public boolean depositoVacio() {
    if(this.deposito<=0){
        return true;
    }else{
        return false;
    }

}

public int vuelta() {
    int adelanta = (int) (Math.random()*10+1);
    if(adelanta <= 1);
    return adelanta; 
}

public String toString() {
    return "** Coche nº"+this.numero+" ** Dep."+this.getDeposito()+" ** vel."+this.getVelocidad();
}
}
    
asked by Rafa Alvarez-Ossorio Martin 29.12.2017 в 11:09
source

1 answer

2

To change a single position is simply a change of value between 2 variables. It would be like this:

//Le vamos a pasar por ejemplo que el kart en la posicion 2 adelanta un puesto
public static void reordenar(int[] p, int numero) { //Siendo p[] el array de posiciones y numero la posición del kart que adelanta
    //Tenemos que p = [10,9,8,7,6,5,4,3,2,1] y posicion = 2

    int aux = p[numero]; //Guardamos el que adelanta en una variable auxiliar
    //En aux guardamos el número 8

    p[numero] = p[numero-1];
    //Sobreescribimos esa posición con el valor anterior, quedaría así el array
    //p = [10,9,9,7,6,5,4,3,2,1];

    p[numero - 1] = aux;
    //Sobreescribimos en la posición numero-1 el valor que habíamos guardado
    // Queda así p = [10,8,9,7,6,5,4,3,2,1];
}

You have to make sure that you will not pass a 0 to the function in the number parameter, although it should not be possible since the one in first position will never overtake.

    
answered by 29.12.2017 / 12:06
source