
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Initialize an ArrayList in Java
In this article, we will learn to initialize an ArrayList in Java. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
Different Approaches
The following are the two different approaches to initialize an ArrayList in Java ?
Using add() Method
One of the most frequent methods of initializing an ArrayList is to use its default constructor and manually add the elements using the add() method.
The following are the steps to initialize an ArrayList with add() method ?
- The ArrayList is created and initialized by using the new ArrayList() constructor.
- Elements are added using the add() method.
- The list is displayed before and after sorting using Collections.sort().
ArrayListmyList = new ArrayList (); myList.add(50);
Example
Below is an example to initialize an ArrayList with add() method ?
import java.util.ArrayList; import java.util.Collections; public class Demo { public static void main(String args[]) { ArrayList<Integer> myList = new ArrayList<Integer>(); myList.add(50); myList.add(29); myList.add(35); myList.add(11); myList.add(78); myList.add(64); myList.add(89); myList.add(67); System.out.println("Points\n"+ myList); Collections.sort(myList); System.out.println("Points (ascending order)\n"+ myList); } }
Output
Points [50, 29, 35, 11, 78, 64, 89, 67] Points (ascending) [11, 29, 35, 50, 64, 67, 78, 89]
Time Complexity: O(n log n), due to sorting with Collections.sort() method.
Space Complexity: O(n), extra space for dynamic resizing in ArrayList.
Using asList() method
Instead of putting elements manually one after the other into an ArrayList, we can initialize it through the asList() method for efficiency. This makes the code structure neater and more compact.
The following are the steps to initialize an ArrayList with asList() method ?
-
Arrays.asList() is used to initialize the ArrayList with predefined elements.
- The ArrayList is then sorted using Collections.sort().
ArrayListmyList = new ArrayList (Arrays.asList(50,29,35,11,78,64,89,67));
Example
Below is an example of initializing an ArrayList with the asList() method ?
import java.util.*; public class Demo { public static void main(String args[]) { ArrayList<Integer> myList = new ArrayList<Integer>(Arrays.asList(50,29,35,11,78,64,89,67)); System.out.println("Points\n"+ myList); Collections.sort(myList); System.out.println("Points (ascending order)\n"+ myList); } }
Output
Points [50, 29, 35, 11, 78, 64, 89, 67] Points (ascending order) [11, 29, 35, 50, 64, 67, 78, 89]
Time Complexity: O(n log n), due to sorting with the elements.
Space Complexity: O(n), new ArrayList is created from Arrays.asList().
Conclusion
Both approaches provide a way to initialize an ArrayList in Java. When you want a dynamic and expandable list with flexibility, using the add() method is advised. But when you want to initialize quickly and statically, Arrays.asList() is a more compact solution.