user2020618
user2020618

Reputation: 45

How to declare my method to receive any kind of list as argument?

public static int menu(String texto, ArrayList<String> opciones) {
    for (int i = 0; i < opciones.size(); i++) {
        System.out.println((i + 1) + ") " + opciones.get(i));
    }

    return solicitarNumero(texto + " [1-"
            + opciones.size() + "]", true, 1, opciones.size());
}

I have this method that receives a text (texto) prompting the user to input an option number and an ArrayList with the options. e.g. {Create entry, remove entry, quit}

solicitarNumero handles the request of the option number and verifies it.

Right now when I'm calling this method using variables of different classes (Franquicia, Gerente, Sistema) I have to go over each ArrayList and create a new one containing that class' toString.

Like so

for (Franquicia franquicia : listaFranquicia) {
    listaDatosFranquicia.add(franquicia.toString());
}

Then I use the method with the new ArrayList.

menu("Ingrese el numero de franquicia "
            + "que desea asignar a este control",
            listaDatosFranquicia) - 1));

Is there a way I could simply do

menu("Ingrese el numero de franquicia "
            + "que desea asignar a este control",
            listaFranquicia) - 1));

Without having to create a new ArrayList?

Upvotes: 3

Views: 119

Answers (3)

m0skit0
m0skit0

Reputation: 25873

I think a much better signature for your menu method would be

public static int menu(final String texto, final List<?> opciones)

This way you can accept any kind of list of objects, and it will rely on how that instance implements toString() method.

Also note I changed ArrayList to List. It is always better to use the interface instead of the specific implementation when possible.

Upvotes: -2

JB Nizet
JB Nizet

Reputation: 691715

Yes. Change the method to

public static int menu(String texto, List<?> opciones) {
    for (int i = 0; i < opciones.size(); i++) {
        System.out.println((i + 1) + ") " + opciones.get(i));
    }

    return solicitarNumero(texto + " [1-"
            + opciones.size() + "]", true, 1, opciones.size());
}

which is basically equivalent to:

public static int menu(String texto, List<?> opciones) {
    for (int i = 0; i < opciones.size(); i++) {
        System.out.println((i + 1) + ") " + opciones.get(i).toString());
    }

    return solicitarNumero(texto + " [1-"
            + opciones.size() + "]", true, 1, opciones.size());
}

unless one of your options is null (in which case the second one will cause a NullPointerException, whereas the first one will print "null").

List<?> means: a list of some unknown type. But since all types extend java.lang.Object, and all you care about is the toString() representation of the objects, you don't really need to know the actual generic type of the list.

Upvotes: 3

Tunaki
Tunaki

Reputation: 137084

Not without changing the method menu.

listaFranquicia is a List<Franquicia> and the method expects a List<String> so you need to convert each Franquicia into a String and make a new list with the result, exactly with the code you have.

It's a bit cumbersome but note that with Java 8, it can be written more simply in a single line:

List<String> listaDatosFranquicia = listaFranquicia.stream()
                                                   .map(Franquicia::toString)
                                                   .collect(Collectors.toList());

Upvotes: 1

Related Questions