Array without repetition

3

How do you not repeat the data with each execution?

Each time I run it, it ends up repeating the dates at the time of execution, as I do for it to sort the dates without repeating them, and when the number of dates available in this collection is over, it displays a message that it is over, if the options?

package DeclaracaoArray;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args) {
        Declaracao_Array d = new Declaracao_Array();
        System.out.println(d.data());
    }

    public String data (){
        List<String> lista = new ArrayList<String>();


        lista.add ( "01/09/2015" );
        lista.add ( "02/09/2015" );
        lista.add ( "03/09/2015");
        lista.add ( "04/09/2015");
        lista.add ( "08/09/2015" );
        lista.add ( "09/09/2015" );
        lista.add ( "10/09/2015");
        lista.add ( "11/09/2015");
        lista.add ( "14/09/2015" );
        lista.add ( "15/09/2015" );
        lista.add ( "17/09/2015");
        lista.add ( "18/09/2015");
        lista.add ( "21/09/2015" );
        lista.add ( "22/09/2015" );
        lista.add ( "23/09/2015");


        Collections.shuffle ( lista );


        return lista.get(0);

    }

}
    
asked by anonymous 01.10.2015 / 14:37

1 answer

3

If you want no repetitions, before returning the value remove from the list:

package DeclaracaoArray;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {

    public static void main(String[] args) {
        Declaracao_Array d = new Declaracao_Array();
        System.out.println(d.data());
    }

    public String data (){
        List<String> lista = new ArrayList<String>();


        lista.add ( "01/09/2015" );
        lista.add ( "02/09/2015" );
        //...
        lista.add ( "23/09/2015");

        //verifique se já não está vazia
        if(lista.isEmpty()) return "Opções esgotadas.";
        else{
            Collections.shuffle ( lista );
            String aux = lista.get(0);
            lista.remove(0);
            return aux;
        }    
    }

}
    
01.10.2015 / 16:08