Is it possible to place objects in an Arraylist as soon as instantiated?

Asked

Viewed 390 times

6

Doubt applies not only in one ArrayList, but in any kind of List. It is possible to add objects to ArrayList soon after we instantiate it, and without having to use the method add()? As can be done with arrays?

Example of what I want to do, only using one array, instead of a Collection:

int[5] array = {1, 2, 3, 4, 5};
  • Renan, thanks for adding to the post, had no knowledge of these methods, will certainly help me to arrive at the solution I need for the problem related to the question.

  • But can you tell me if the methods of this question you mentioned are available in Java 8? Or is it just Java 9 itself?

  • List.of is present only in Java 9. Arrays.asList for the longest time: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

3 answers

7

If you want a simple changeable list, nothing stops you from doing that:

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

class MinhasListas {
    public static <E> List<E> of(E... elementos) {
        return new ArrayList<>(Arrays.asList(elementos));
    }
}

An example of use would be like this:

class Main {
    public static void main(String[] args) {
        List<String> x = MinhasListas.of("a", "b", "c");
        List<Integer> y = MinhasListas.of(1, 2, 3);
    }
}

And this then works with any version of Java from 5 up. So even if you’re stuck in an old version, you won’t have any problems.

  • Thank you very much, I was wanting to create a method to address the lack of, since I use Java 8. This is for sure!

5

From Java 9, you have static methods on the interface List for that reason:

List<String> abc = List.of("a", "b", "c");

But the use of this method creates a list that will be immutable, and can not be done all that can be done with a list "normal".

4


Not possible in Java.

It is possible initialize the array with a specific size to avoid needing relocations when the list gets larger than the array available.

The options cited in reply linked in comment above and other responses here do not generate a real list, generates something that looks like a list, but it is immutable and can not do everything expected from a list. And it’s not something efficient, it’s just a facilitator.

If this fits, then one array solves your problem and it can be initialized as you wish. Carrying the other solutions or do not solve the problem mentioned or the problem can be solved in a much more elegant and performative way.

It is possible to create a method that creates a real list from a sequence of arguments, but it is also not efficient. It is actually very inefficient because there will be two copies, one to initialize the array which will be passed to the method and another to copy to the list.

In C# you can do this, but it’s only syntactic sugar. In C++ you can do it, in certain circumstances efficiently.

  • Thank you for the exclamation.

  • I don’t know if the answer is correct, you saw that question Maniero?

  • @Renan is technically correct, I improved to show why.

  • The case presented in the linked reply in the comment above would suit me, because I want to create this list only to inform as argument to a method that will save all objects present in the List in a database, will not modify the List in any way, then the linked question would help me to arrive at the solution. However, I cannot use Java 9 in the project. Maniero’s response is so far the most appropriate. Thank you to Renan for presenting the List.of method to the discussion, will help anyone who has this problem in the future, and thank you to Maniero for the removal.

  • @Brunno see on [tour] the best way to say thank you.

  • @Maniero Thanks for remembering. I used to use Stack Overflow before, I know the function of accepting the answer, only I usually forget to dial because it has that minimum waiting time.

  • @Brunno you can vote for everything too.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.