I have the following class:
package menu;
import java.util.List;
public class Menu
{
private String titulo;
private List<String> opciones;
public Menu(String titulo, List<String> opciones)
{
this.titulo = titulo;
this.opciones = opciones;
}
public void mostrar()
{
System.out.println();
System.out.println(titulo);
for (int i = 0; i < opciones.size(); i++)
{
System.out.println(i + 1 + ". " + opciones.get(i));
}
System.out.println();
}
}
And in the method main
I have created two variables as you can see below:
import java.util.Arrays;
import java.util.List;
import menu.Menu;
public class Principal
{
public static void main(String[] args)
{
String tituloMenu = "Menú Principal";
List<String> opcionesMenu = Arrays.asList("Jugar", "Ver instrucciones", "Salir");
//resto del código
}
}
My problem is that I am looking for an easier way to initialize the opcionesMenu
. The way in which I initialize said variable was obtained by researching on SO, however I have not been able to conclude if it is possible ArrayList
to initialize a C# style:
var opcionesMenu = new List<string> { "Jugar", "Ver instrucciones", "Salir" };
The other problem is that I don't understand very well how the initialization of the ArrayList
one I put in the previous code works. Basically I have two questions: why is it necessary to use Arrays
? and what exactly does it mean to make use of the method asList
?
Thanks in advance for possible comments and/or answers.