Java ArrayList Collection

What is ArrayList Collection in java?

  • ArrayList is one of the collection class in java.
  • ArrayList class extends AbstractList and implements the List interface.
  • ArrayList supports dynamic arrays that can grow as needed, but arrays in java are fixed length ( once array is created that cannot grow or shrink).
  • ArrayList is a variable length array of object references.
  • ArrayList can dynamically increase or decrease in size of the collection.
  • ArrayList is created with an initial size, when the size is exceeded, the size is automatically increased. when the objects are removed in ArrayList, the size may be reduced.
  • Legacy class 'Vector' is also supports the dynamic arrays.

ArrayList Constructors Types

ArrayList()
This constructor builds an empty array list.
ArrayList(Collection collection)
This constructor builds an array list that is initialized with the elements of the collection.
ArrayList(int capacity)
This constructor builds an array list that has specified the initial capacity. here capacity is the size of the underlying array that is used to store the elements. Capacity grows automatically as elements are added to an array list.

ArrayList Methods

  • add method is used to add new object in the array list.
    boolean add(Object)
    
    adds new object in the array list.
    boolean add(int, Object)
    
    adds new object at the specified index of array list.
  • addAll method is used to add the collection of objects in the array list.
    boolean addAll(Collection) 
    
    boolean addAll(int, Collection)
    
    adds the collection of objects starting from the specified index of array list. Returns true if object was added to the ArrayList, otherwise false.
  • clear method is used to remove all the elements in the array list.
    void clear()
    
  • contains method returns true if object is an element of the array list, otherwise returns false.
    boolean contails(Object)
    
  • containsAll method returns true if array list contains all the elements of provided collection, otherwise returns false.
    boolean containsAll(Collection)
    
  • equals method returns true if collection and object are equal.
    boolean equals(Object)
    
  • hashCode method returns hash code for the collection.
    int hashCode()
    
  • isEmpty method returns true if collection is empty. otherwise returns false.
    boolean isEmpty()
    
  • iterator method returns an iterator for the collection.
    Iterator iterator()
    
  • remove method removes one instance of object from the collection. returns true if element was removed, otherwise returns false.
    boolean remove(Object)
    
  • removeAll method removes all elements of provided collection from the invoking collection. Returns true if the elements were removed, otherwise returns false.
    boolean removeAll(Collection)
    
  • retainAll method removes all elements from the invoking collection except those elements in provided collection. Returns true if elements were removed, otherwise return false.
    boolean retainAll(Collection)
    
  • size method returns the number of elements in the collection.
    int size()
    
  • toArray mehod returns an array that contains all the elements in the collection.
    Object[] toArray()
    

Creating ArrayList and Adding Elements in Java Program

// Codingpointer Demonstrates ArrayList                                         
                                                                                
import java.util.*;                                                             
                                                                                
class ArrayListTest {                                                           
 public static void main(String args[]) {                                       
  // Creates ArrayList                                                          
  ArrayList arrList = new ArrayList();                                          
  System.out.println("Array size default: " + arrList.size());                  
                                                                                
  // add array list elements.                                                   
  arrList.add(1);                                                               
  arrList.add(2);                                                               
  arrList.add(3);                                                               
  arrList.add(4);                                                               
  arrList.add(5);                                                               
  arrList.add(6);                                                               
  arrList.add(2, 4.5);                                                          
                                                                                
  System.out.println("Size of array list: "+ arrList.size());                   
  // prints array list elements                                                 
  System.out.println("Elements in array list: " + arrList);                     
 }                                                                              
}    
Output:
$ javac ArrayListTest.java 
Note: ArrayListTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ java ArrayListTest 
Array size default: 0
Size of array list: 7
Elements in array list: [1, 2, 4.5, 3, 4, 5, 6]

Remove Elements in ArrayList Java Program

// Codingpointer Demonstrates ArrayList                                         
                                                                                
import java.util.*;                                                             
                                                                                
class ArrayListTest {                                                           
 public static void main(String args[]) {                                       
  // Creates ArrayList                                                          
  ArrayList arrList = new ArrayList();                                          
  System.out.println("Array size default: " + arrList.size());                  
                                                                                
  // add array list elements.                                                   
  arrList.add("1");                                                             
  arrList.add("1");                                                             
  arrList.add("3");                                                             
  arrList.add("4");                                                             
  arrList.add("5");                                                             
  arrList.add("6");                                                             
  arrList.add(2, "4.5");                                                        
                                                                                
  System.out.println("Size of array list: "+ arrList.size());                   
  // prints array list elements                                                 
  System.out.println("Elements in array list: " + arrList);                     
                                                                                
  // remove array list elements                                                 
  arrList.remove("1");                                                          
  arrList.remove(4);                                                            
  System.out.println("Size of array list after deletions: "+arrList.size());    
  System.out.println("Elements in array list: " + arrList);                     
 }                                                                              
} 
Output:
$ java ArrayListTest 
Array size default: 0
Size of array list: 7
Elements in array list: [1, 1, 4.5, 3, 4, 5, 6]
Size of array list after deletions: 5
Elements in array list: [1, 4.5, 3, 4, 6]

Obtain Array from ArrayList Java Program

// Codingpointer Demonstrates ArrayList                                         
                                                                                
import java.util.*;                                                             
                                                                                
class ArrayListTest {                                                           
 public static void main(String args[]) {                                       
  // Creates ArrayList                                                          
  ArrayList arrList = new ArrayList();                                          
  System.out.println("Array size default: " + arrList.size());                  
                                                                                
  // add array list elements.                                                   
  arrList.add(new Integer(5));                                                  
  arrList.add(new Integer(1));                                                  
  arrList.add(new Integer(3));                                                  
  arrList.add(new Integer(2));                                                  
  arrList.add(new Integer(4));                                                  
                                                                                
                                                                                
  System.out.println("Size of array list: "+ arrList.size());                   
  // prints array list elements                                                 
  System.out.println("Elements in array list: " + arrList);                     
                                                                                
  // get array                                                                   
  Object arr[] = arrList.toArray();                                             
  int sum = 0;                                                                  
  // sum array                                                                  
  for(int i=0; i<arr.length; i++) {                                             
   sum += ((Integer) arr[i]).intValue();                                        
  }                                                                             
  System.out.println("Sum is: " + sum);                                         
 }                                                                              
}    
Output:
$ java ArrayListTest 
Array size default: 0
Size of array list: 5
Elements in array list: [5, 1, 3, 2, 4]
Sum is: 15
Java Examples

GCD and Rational Number Java Program

Java Queue Program using exception

Java Stack Program using exception

Addition of three integers java program

Biggest of three integers java program

Fibonacci numbers java program

Arithmetic Operations Menu java program

Second Smallest Element In Array Java Program

Transpose Of A Matrix Java Program

Java Program to Display triangle of stars (*)

Java Programming Prints Product Tables

Java Program to Display triangle of numbers

Java Program to Get Current Date

Java Program to Find Character Vowel or Consonent

Java Program to Compute HCF and LCM

Java Program to Sum the Command Line Integer Arguments

Java Programm to Multiply the Command Line Integer Arguments

Java Program to Find Multiplication of Command Line Floating Point Numeric Arguments

Java Program to Check String Contains or Not

Java Program to Get Grade Description using Switch

Java Program for CSV File Reader

Java Program to Find Character Frequency Count in String using for each

Java Program to Find Min from Integer Array by Passing Array to Method

Java Program Linear Search

Binary Search Java Program

Ternary Search Java Program

Java Program to Generate Range of Numbers

Java Program to Generate Range of Characters

Java Program to Compute Square Root Value

Java Program to Check Number is Positive or Negative

Java Program to Check Number is Odd or Even

Java Program to Compute Plot Area

Java Program to Convert Number of Days into Years

Java Program to Check a Year is Leap Year, Century Year or Not

Java Program to Check a Character is Digit, Letter or neither digit nor letter

Java Program to Check a Number is Palindrome or not

Java Program to Sum Two Matrix

Java Program to Compute Power

Java Program to Check Number is an Armstrong Number or not

Java Program for Temperature Unit Conversions

Java Program to Generate Random Numbers in Specified Range

Java Program to Compute Sum of Digits and Product of Digits

Java Program to Compute Reverse Number

Java Programming Computes Factorial Value

Java Programming Checks Prime Number or not

Java Program to Compute Harmonic Series

Java Program Generate Floyd's Triangle

Java Program to Reverse String

Java Program to Check Palindrome String or not

Java Program to Open Notepad

Java Program to Search String using RegEx Pattern

Java Program to Search Word in String

Java Program to Get System Environment Variables

Java Program to Get IP Address of Server Name

Java Program for Arrays Sorting and Reverse using sort method

Java Program for Bubble Sorting

Java Program for Selection Sorting

Java Program for Insertion Sorting

Java Program for Merge Sorting

Java Program for Quick Sorting

Java Program for Counting Sort

Java Program for Radix Sorting

Java Program for Sorting Array of Strings

Java Program for String Characters Sorting

Java Program to Sum First N Numbers

Java Program to Product First N Numbers

Java Program to get URL details

Java Program to get URL HTML Content

Java Program to get URL details

Java Program to get URL HTML Content

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^