

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you create an empty list in Java?
A List of elements can be created using multiple ways.
Way #1
Create a List without specifying the type of elements it can holds. Compiler will throw warning message for it.
List list = new ArrayList();
Create a List and specify the type of elements it can holds.
Way #2
List<CustomObject> list = new ArrayList<>();
Example
Following is the example to explain the creation of List objects −
package com.tutorialspoint; import java.util.*; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Zara"); list.add("Mahnaz"); list.add("Ayan"); System.out.println("List: " + list); } }
Output
This will produce the following result −
List: [Zara, Mahnaz, Ayan]
- Related Questions & Answers
- How do you create a list in Java?
- How do you empty an array in C#?
- How do you create a list with values in Java?
- How do you create a list from a set in Java?
- How do I empty a list in Java?
- How to create an empty list in Python?
- How do you add an element to a list in Java?
- How do you check a list contains an item in Java?
- How do you copy a list in Java?
- How do you convert list to array in Java?
- How do you make a list iterator in Java?
- How do you copy an element from one list to another in Java?
- How do you check if an element is present in a list in Java?
- How do you get the index of an element in a list in Java?
- How do you convert an ArrayList to an array in Java?
Advertisements