Add Multiple Items to an Already Initialized Arraylist in Java

Add multiple items to an already initialized arraylist in Java

If you have another list that contains all the items you would like to add you can do arList.addAll(otherList). Alternatively, if you will always add the same elements to the list you could create a new list that is initialized to contain all your values and use the addAll() method, with something like

Integer[] otherList = new Integer[] {1, 2, 3, 4, 5};
arList.addAll(Arrays.asList(otherList));

or, if you don't want to create that unnecessary array:

arList.addAll(Arrays.asList(1, 2, 3, 4, 5));

Otherwise you will have to have some sort of loop that adds the values to the list individually.

Adding multiple items at once to ArrayList in Java

Use Collections.addAll:

Collections.addAll(integerArrayList, 1, 2, 3, 4);

Initialization of an ArrayList in one line

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

The catch is that there is quite a bit of typing required to refer to that list instance.

There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):

ArrayList<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};

However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.

What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):

List<String> list = ["A", "B", "C"];

Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.

How to add many values to an arraylist at once?

in java 8 :

List<String> yourList= Arrays.asList(a,b,c,d,e);

you can make it shorter by static import: import static java.util.Arrays.asList;

in java 9 you can use list of:

List<String> yourList = List.of(a,b,c,d,e);

and in java 10 and later there is a keyword named var:

var yourList = List.of(a,b,c,d,e);

in this way yourList will be immutable so it can not be changed.

you can also use Stream:

List<String> yourList= Stream.of(a,b,c,d,e).collect(toList());

or

Stream<String>  yourList= Stream.of(a,b,c,d,e);

Add multiple numbered objects to ArrayList

Use an array:

String[] strs = { "abc","123","zzz" };

for(int i = 0; i < strs.length; i++){
list.add(strs[i]); //something like this?
}

This idea is so popular that there's built-in methods to do it. For example:

  list.addAll( Arrays.asList(strs) );

will add your array elements to an existing list. Also the Collections class (note the s at the end) has static methods that work for all Collection classes and do not require calling Arrays.asList(). For example:

Collections.addAll( list, strs );
Collections.addAll( list, "Larry", "Moe", "Curly" );

If you just want a list with only the array elements, you can do it on one line:

  List<String> list = Arrays.asList( strs );

Edit: Many other classes in the Java API support this addAll() method. It's part of the Collection interface. Other classes like Stack, List, Deque, Queue, Set, and so forth implement Collection and therefore the addAll() method. (Yes some of those are interfaces but they still implement Collection.)

Adding Multiple Values in ArrayList at a single index

@Ahamed has a point, but if you're insisting on using lists so you can have three arraylist like this:

ArrayList<Integer> first = new ArrayList<Integer>(Arrays.AsList(100,100,100,100,100));
ArrayList<Integer> second = new ArrayList<Integer>(Arrays.AsList(50,35,25,45,65));
ArrayList<Integer> third = new ArrayList<Integer>();

for(int i = 0; i < first.size(); i++) {
third.add(first.get(i));
third.add(second.get(i));
}

Edit:
If you have those values on your list that below:

List<double[]> values = new ArrayList<double[]>(2);

what you want to do is combine them, right? You can try something like this:
(I assume that both array are same sized, otherwise you need to use two for statement)

ArrayList<Double> yourArray = new ArrayList<Double>();
for(int i = 0; i < values.get(0).length; i++) {
yourArray.add(values.get(0)[i]);
yourArray.add(values.get(1)[i]);
}

How to add single item to arraylist for multiple time with out using for loop

You can use Collections's static <T> List<T> nCopies(int n, T o):

List<String> list = Collections.nCopies("4.5",count);

However, that would create an immutable List<String>. In order to obtain a mutable List, you'll have to pass that list to ArrayList's constructor, so I'm not sure the end result will be much faster than your current loop.

List<String> mylist = new Arraylist<>(Collections.nCopies("4.5",count));

Initial size for the ArrayList

You're confusing the size of the array list with its capacity:

  • the size is the number of elements in the list;
  • the capacity is how many elements the list can potentially accommodate without reallocating its internal structures.

When you call new ArrayList<Integer>(10), you are setting the list's initial capacity, not its size. In other words, when constructed in this manner, the array list starts its life empty.

One way to add ten elements to the array list is by using a loop:

for (int i = 0; i < 10; i++) {
arr.add(0);
}

Having done this, you can now modify elements at indices 0..9.



Related Topics



Leave a reply



Submit