
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How do I add multiple items to a Java ArrayList in single statement?
We can utilize Arrays.asList() method to get a List of specified elements in a single statement.
Syntax
public static <T> List<T> asList(T... a)
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
Type Parameter
T − The runtime type of the array.
Parameters
a − The array by which the list will be backed.
Returns
A list view of the specified array
In case we use Arrays.asList() then we cannot add/remove elements from the list. So we use this list as an input to the ArrayList constructor to make sure that list is modifiable.
Example
The following example shows how to create a List with multiple items in a single statement.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); // print the list System.out.println(list); list.add("D"); System.out.println(list); } }
Output
This will produce the following result −
[A, C, D] [A, B, C, D]
- Related Articles
- How to alter multiple columns in a single statement in MySQL?
- How do I order a list and add position to its items in MongoDB?
- How do I insert multiple values in a column with a single MySQL query?
- How can I add items to a spinner in Android?
- How do I remove multiple elements from a list in Java?
- How to assign same value to multiple variables in single statement in C#?
- How to add items in a JComboBox on runtime in Java
- How do you remove multiple items from a list in Python?
- How do I insert an item between two items in a list in Java?
- Remove duplicate items from an ArrayList in Java
- How to add items to an array in java dynamically?
- How do I add an element to an array list in Java?
- MySQL query to increase item value price for multiple items in a single query?
- Add a single element to a LinkedList in Java
- Insert multiple sets of values in a single statement with MySQL?

Advertisements