Arraylist in java


Is it possible that when you create an ArrayList it already has a value inside it?


//creo el arraylist para almacenar luchadores que son parte de otra clase
private ArrayList<Luchador> cantLuchadores= new ArrayList<Luchador>();

and that cantluchadores.get(0) already have a fighter created

asked by DIEGO IGNACIO VEGA VERGARA 21.04.2018 в 21:48

3 answers


Since Java 9 you have the method of in the List interface. It allows you to create an unmodifiable list of elements. The use would be like this:

private List<Luchador> cantluchadores = List.of(new Luchador(), new Luchador());

You have two problems with this:

  • Your variable changes from type ArrayList to type List . If you use own methods of ArrayList that do not conform with the interface List , your code will not compile. It is true that it is recommended to use List better than ArrayList in the vast majority of cases, because List usually has everything you need, so I do not think it affects you.

  • The list returned by the method is unmodifiable, so you can not add or remove elements, but you can modify your wrestlers , if you want to have a modifiable list, you can do new ArrayList<>(List.of(new Luchador(), new Luchador()));

  • answered by 22.04.2018 в 15:45

    List that does not change in size

    Another alternative in case you need to have a partially mutable list is to use the static method Arrays.asList

    List<Luchador> luchadores= Arrays.asList(new Luchador("El Santo"), new Luchador("El Cavernario"), new Luchador("Blue Demon"));

    The size of your list will not change , but you can replace the elements of your list using the set java.util.ArrayList.set(int indice, E elemento) method.

    For example, if you wanted to replace the fighter "El Cavernario" with "El cavernario Galindo", you could do something like this:

    luchadores.set(1,new Luchador("El Cavernario Galindo"));

    This method should work from Java 7.

    Fully manipulated list

    If you want a list that you can fully manipulate at your whim, you can use Stream and convert it to the list as follows:

    List<Luchador> luchadores = Stream.of((new Luchador("El Santo"), new Luchador("El Cavernario"), new Luchador("Blue Demon")).collect(toList());

    and without problems you could add any other

    luchadores.add(new Luchador("El Bulldog"));

    This method should work from Java 8.

    Modifiable list for obsolete versions

    There is another traditional way of doing it for already deprecated versions of Java but I recommend not to use it , it is known as double-key initialization, it has the defect that you create anonymous internal classes.

    List<Luchador> list = new ArrayList<Luchador>() {{
                add(new Luchador("El Santo"));
                add(new Luchador("El Cavernario"));
                add(new Luchador("Blue Demon"));
    answered by 24.04.2018 в 01:20

    The only option you have is to create your list cantLuchadores from another collection, using the constructor of the class ArrayList that receives as a parameter a type Collection .

    private ArrayList<Luchador> cantLuchadores= new ArrayList<>(unaCollection);

    Otherwise, the list will be created empty (without elements), which will cause that if you try to access any position (eg cantluchadores.get(0) ) an exception of type IndexOutOfBoundsException is thrown.

    answered by 21.04.2018 в 23:10