
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 a List in Java
In this article, we will learn to initialize a list in Java. A list is an ordered collection that allows us to store and access elements sequentially. It contains index-based methods to insert, update, delete, and search the elements. It can also have duplicate elements.
Problem Statement
Given a scenario where you need to create and initialize a list in Java, the goal is to understand and implement various approaches to initialize lists effectively.
Input
Initialize an empty list and add elements ?
"Apple", "Banana", "Cherry"
Output
["Apple", "Banana", "Cherry"]
Different Approaches
Following are the different approaches to Initialize a list in Java ?
Naive Approach
In the naive approach, the list initialization and population are done directly within the main() method. The lists are declared and elements are added sequentially using the add() method.
Algorithm
Following are the steps to initialize a list in Java using a naive approach ?
- Step 1 - START
- Step 2 - Declare an integer list namely integer_list and a string list namely string_list
- Step 3 - Define the values.
- Step 4 - Use the List integer_list = new ArrayList() to initialize the integer list.
- Step 5 - Use List string_list = new ArrayList() to initialize the integer list.
- Step 6 - Use the function add() method to add items to the list.
- Step 7 - Display the result
- Step 8 - Stop
Example
Below is an example of initializing a list in Java using a naive approach ?
import java.util.*; public class Demo { public static void main(String args[]) { System.out.println("\nInitializing an integer list"); List<Integer> integer_list = new ArrayList<Integer>(); integer_list.add(25); integer_list.add(60); System.out.println("The elements of the integer list are: " + integer_list.toString()); System.out.println("\nInitializing a string list"); List<String> string_list = new ArrayList<String>(); string_list.add("Java"); string_list.add("Program"); System.out.println("The elements of the string list are: " + string_list.toString()); } }
Output
Initializing an integer list The elements of the integer list are: [25, 60] Initializing a string list The elements of the string list are: [Java, Program]
Time Complexity: O(1), for each operation; O(n) overall due to printing.
Space Complexity: O(n), where n is the number of elements in the lists.
Object-Oriented Approach
In the object-oriented approach, list initialization and population are encapsulated into separate functions, following the principles of modularity and reusability. This method promotes cleaner and more maintainable code by organizing operations into distinct functions.
Following are the steps to initialize a list in Java using the object-oriented approach ?
-
Package Import: The java.util package is imported to use utility classes like ArrayList for dynamic list management.
-
Modular Structure: Two separate methods, initialize_int_list() and initialize_string_list(), handle the creation and population of integer and string lists.
-
ArrayList Initialization: In each method, an ArrayList is created to store values, using generics for type safety and flexibility.
-
Element Addition: Elements are added using the add() method of ArrayList, which appends the values to the list efficiently.
- Output Display: The toString() method is used to generate a string representation of the list, which is printed to display the contents of the list.
Example
Below is an example of initializing a list in Java using the object-oriented approach ?
import java.util.*; public class Demo { static void initialize_int_list(){ List<Integer> integer_list = new ArrayList<Integer>(); integer_list.add(25); integer_list.add(60); System.out.println("The elements of the integer list are: " + integer_list.toString()); } static void initialize_string_list(){ List<String> string_list = new ArrayList<String>(); string_list.add("Java"); string_list.add("Program"); System.out.println("The elements of the string list are: " + string_list.toString()); } public static void main(String args[]) { System.out.println("\nInitializing an integer list"); initialize_int_list(); System.out.println("\nInitializing a string list"); initialize_string_list(); } }
Output
Initializing an integer list The elements of the integer list are: [25, 60] Initializing a string list The elements of the string list are: [Java, Program]
Time Complexity: O(1), for each operation; O(n) overall due to printing.
Space Complexity: O(n), where n is the number of elements in the lists.