![]() |
ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the concepts of Arrays and Lists in Java programming. In this article, we will learn to declare an ArrayList with values in Java. Program to declare an ArrayList with ValuesIt is the easiest and most direct method to add values while declaration. There is a basic way to declare an ArrayList with values is by Adding elements one by one by using the add() method. Syntax:ArrayList<Integer> arrayList = new ArrayList<>();arrayList.add(1);arrayList.add(2); Below is the implementation of Declare an ArrayList with Values in Java: Java
Output
ArrayList: [5, 1, 2, 4] Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |