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.
Let's go in parts to understand each of the questions you raise.
In java there are several options of getting an instance of an object that implements the interface
List
and inserting "initial" data into it, which is the data type of your variableopcionesMenu
. But each one depends on what you want to achieve, since each one has its characteristics.1 - Using the operator
new
.This variant is the classic or traditional and also the most verbose, which is sometimes a bit annoying.
2 - From an array using the class
Arrays
(Method you use).This option can be useful if you already have an external array with the elements you want to insert in the list or if you want to save a bit of code, such as the successive calls to the method
add()
in point 1. On the other hand, you have to be very clear and always present , which list is obtained like this:a. it's fixed in size, so you can't add or remove items from it. Both operations throw an exception of type
UnsupportedOperationException
.b. if variant 1 is used, the resulting list would be a view on the original array, which means that if we, for example, sort the list, the array will be sorted as well.
3 - Using the factory methods of the interface
List
(as of Java 9). These methods are also found in interfacesSet
andMap
This option allows us to create a list in a very non-verbose way. It is very similar to if we use the class
Arrays
, but it is more understandable to understand that what we are creating is a list if we do it from the interfaceList
, instead of using the classArrays
, which tends to be more associated with working with arrays. Additionally, we must know that the list we obtain will be immutable, so as in point 2, we will not be able to add or remove elements, but unlike said point, we will not be able to perform operations such as ordering on it either. With which we can only make queries about it (get the element in a position, go through it, etc).The use of the class
Arrays
is not mandatory. What happens is that before the arrival of the factory methods for collections with Java 9, the least verbose option we had to obtain a list from a finite set of predefined elements, was the use of the methodasList
of said class. In point 2 above I explain the use of this method and its characteristics.It is not necessary to use the Arrays class. You can use a list, for example the ArrayList class.
The traditional way of feeding it would be:
I'll pass you the league of that class:
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
You can also use an array itself and avoid the list:
I don't know if that's easy for you.
Greetings.