lunes, 17 de febrero de 2014

Unidad 3: Ejercicio 14 el array anterior ordenado

 Visualizar el contenido del vector del ejercicio anterior ordenado alfabéticamente. 


package unidad3ejercicio14;

import java.util.Random;
import java.util.Arrays;

/**
*
* @author juan
*/
public class Unidad3Ejercicio14 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

  String [] nombre = {"Daniel", "Yago", "Anxo", "Borja", "Diego", "Manolo", "Pepe", "David", "Pablo", "Miguel"};
String [] apellido1 = {"Gonzalez", "Perez", "Crespo", "Valverde", "Dominguez", "Castro", "Martinez", "Lopez", "ALvarez", "Bernardez"};
String [] apellido2 = {"Barja", "Gutierrez", "Fernandez", "Noguerol", "Outes", "Conde", "Rodriguez", "Alvarez", "Garcia", "Arean"};
        String [] nombresApellidos = new String[30];
        String [] nombresAleatorios = new String[5];
        Random aleatorio = new Random();
       
        System.arraycopy(nombre, 0, nombresApellidos, 0, nombre.length);
        System.arraycopy(apellido1, 0, nombresApellidos, nombre.length, apellido1.length);
        System.arraycopy(apellido2, 0, nombresApellidos, (nombre.length+apellido1.length), apellido2.length);
       
        for (int i = 0; i < nombresAleatorios.length; i++) {
            nombresAleatorios[i]=nombre[aleatorio.nextInt(nombre.length)]+" " + apellido1[aleatorio.nextInt(apellido1.length)]+" "+apellido2[aleatorio.nextInt(apellido2.length)]; ;
           
        }
        System.out.println("Nombres Generados aleatoriamente");
        System.out.println("================================");
        for (String string : nombresAleatorios) {
            System.out.println(string);
        }
       
       
        //Ahora copiándolos desde nombresapellidos
       
        for (int i = 0; i < nombresAleatorios.length; i++) {
            nombresAleatorios[i]=nombresApellidos[aleatorio.nextInt(nombre.length)]+" " + nombresApellidos[aleatorio.nextInt(apellido1.length)+nombre.length]+" "+nombresApellidos[aleatorio.nextInt(apellido2.length)+apellido1.length+nombre.length];
           
        }
        System.out.println("Nombres Generados aleatoriamente");
        System.out.println("================================");
        for (String string : nombresAleatorios) {
            System.out.println(string);
        }
       
        /* Comparación de cadenas
        nombresAleatorios[0]=nombresAleatorios[2];
        System.out.println(nombresAleatorios[0]==nombresAleatorios[2]);
        System.out.println(nombresAleatorios[0].equalsIgnoreCase(nombresAleatorios[2]));
        *
        */
        System.out.println("Nombres Generados aleatoriamente");
        System.out.println("================================");
        int cont=0;
        int cont2;
       
        while (cont<nombresAleatorios.length)
        {
            nombresAleatorios[cont]=nombresApellidos[aleatorio.nextInt(nombre.length)]+" " + nombresApellidos[aleatorio.nextInt(apellido1.length)+nombre.length]+" "+nombresApellidos[aleatorio.nextInt(apellido2.length)+apellido1.length+nombre.length];
            cont2=0;
            while (cont!=cont2 && !nombresAleatorios[cont].equalsIgnoreCase(nombresAleatorios[cont2])) {
                ++cont2;
            }
            if (cont==cont2) cont++;
        }
         for (String string : nombresAleatorios) {
            System.out.println(string);
        }
        
         Arrays.sort(nombresAleatorios);
         System.out.println("Nombres Generados aleatoriamente");
        System.out.println("================================");
        for (String string : nombresAleatorios) {
            System.out.println(string);
        }
    }
}

No hay comentarios: