What is an ArrayList in Java?

ArrayList in Java:

A resizable array that is part of Java. until the package is the ArrayList class. Built-in arrays have a fixed size, but ArrayLists have a dynamic size. An ArrayList can have elements added and removed as needed, which aids in memory management for the user.



The java.util package contains the ArrayList class, a resizable array. In Java, a built-in array and an ArrayList vary in that an array's size cannot be changed; instead, a new array must be created if you want to add or remove entries from an existing one.

Example:-

Java ArrayList Example
**********************
import java.util.*;  
 public class JavaOneWorldArrayList{ 
 
 public static void main(String args[]){
  
   /*Creating arraylist*/
   ArrayList<String> list=new ArrayList<String>(); 
   
     /*Adding object in arraylist*/
    
      list.add("Java");    
      list.add("Android");    
      list.add("Kotlin");
      list.add("Web");    

      /*Printing the arraylist object*/
   
      System.out.println(list);  
 }  
}  

O/P - [Java, Android, Kotlin, Web]

**********************

What is ArrayList used for in Java?

Java's ArrayList holds collections of elements that change in size dynamically. An ArrayList automatically expands in size when new elements are added, unlike Arrays, which have a fixed size. Java's collection foundation includes ArrayList, which implements the List interface.

****************************************
For more visit - www.javaoneworld.com

No comments:

Post a Comment