[Java] How to add data to List (add, addAll)

Programming study diary

October 22, 2020 I didn't understand how to use addAll, so I will summarize how to add data to List.

How to use add to add a value

The add method can add a value. You can insert at the end or position of the List.

Add to the end


List<Integer> list = new ArrayList<Integer>();
//Add value 1 to the end of integer type List
list.add(1);

To determine the insertion position, specify two arguments. The first argument specifies the place to insert, and the second argument specifies the value to insert.

Determine the insertion position


List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
list.add(4);
//Put 2 between 1 and 3 in List
list.add(1,,2);

How to use addAll to add values at once

addAll is a method that allows you to add multiple values at once.

//Prepare two Lists and add the value of list2 to list
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);

List<Integer> list2 = new ArrayList<Integer>();
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
list.addAll(list2);

//The result is similar to the above code without defining a List
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.addAll(Arrays.asList(5, 6, 7, 8));

Like the add method, the addAll method can specify the position to add. The processing method is the same, and the argument is specified.

Determine the insertion position


List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
 
List<Integer> list2 = new ArrayList<Integer>();
list2.add(5);
list2.add(6);
list2.add(7);
list2.add(8);
list.addAll(1, list2);

References

Create a list from a Java array (addAll / asList) (https://itsakura.com/java-collections) [Basics of Java in 3 minutes] How to add data to List (add, addAll)

Recommended Posts

[Java] How to add data to List (add, addAll)
[Java] How to use List [ArrayList]
Add empty data to the top of the list
[Java] How to operate List using Stream API
How to lower java version
[Java] How to use Map
Java --How to make JTable
How to add ActionText function
How to use java Optional
java: How to write a generic type list [Note]
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
How to create a data URI (base64) in Java
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to add HDD to Ubuntu
List data structure [Java / Scala]
How to use JSON data in WebSocket communication (Java, JavaScript)
For Java beginners: List, Map, Iterator / Array ... How to convert?
Add multi-letter watermark to Java Word
How to add columns to a table
Studying Java # 6 (How to write blocks)
How to use Spring Data JDBC
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
[How to install Spring Data Jpa]
How to use Java HttpClient (Post)
[Java] How to use join method
How to learn JAVA in 7 days
[Rails] How to add new pages
[Processing × Java] How to use variables
[Java] How to create a folder
How to decompile java class files
[Java] How to use LinkedHashMap class
[Java] Convert 1-to-N List to Map
[JavaFX] [Java8] How to use GridPane
How to write Java variable declaration
Add watermark to Java to PDF document
How to use class methods [Java]
How to use classes in Java?
How to name variables in Java
How to pass Oracle Java Silver
[Java] Add WordArt to Word document
How to add the delete function
How to turn Iterator Dojo (Java)
java Eclipse How to debug javaScript
[Java] Conversion from array to List
[Processing × Java] How to use arrays
How to make a Java array
How to use Java lambda expressions