Do not repeat letters (char) in an ASCII array

3

You had to create a 5x5 array by printing random characters from the ASCII table.

public class ExercicioClass01g {
    static Scanner ler = new Scanner(System.in);

    public static char mat[][] = new char[5][5];

    public static void gera(int lin, int col){
        Random gerador = new Random();
        int ctlin,ctcol;
        for(ctlin=0;ctlin<lin;ctlin++)
            for(ctcol=0;ctcol<col;ctcol++)
                mat[ctlin][ctcol]=(char)(gerador.nextInt(25)+65);

    }   

    public static void exibe(int lin, int col){
        int ctlin,ctcol;
        for(ctlin=0;ctlin<lin;ctlin++){
            for(ctcol=0;ctcol<col;ctcol++)
                System.out.print(mat[ctlin][ctcol]+" ");
            System.out.println();
        }       
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        gera(5, 5);
        exibe(5,5);
    }

I was able to print but I can not make the letters ( char ) not repeat in the array.

    
asked by anonymous 29.05.2016 / 01:09

1 answer

2

Instead of using random generation use the Fisher-Yates algorithm. It would be nice to create a function that creates the list and then use it as you want:

public static List<Integer> randomNumbers(int start, int end, int count) {
    List<Integer> lista = new ArrayList<>(end - start + 1);
    for (int i = start; i <= end; i++) {
        lista.add(i);
    }
    Collections.shuffle(lista);
    lista = lista.subList(0, count);
    return lista;
}

See working on ideone and on CodingGround .

    
29.05.2016 / 01:29