Java Programming 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 Programming 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 Programming Gets Current Date

Java Programming Finds Character Vowel or Consonent

Java Programming HCF and LCM Computation

Java Programming Sum of Command Line Integer Arguments

Java Programming Multiplication of Command Line Integer Arguments

Java Programming Multiplication of Command Line Floating Point Numeric Arguments

Java Programming String Contains or Not

Java Programming Gets Grade Description using Switch

Java Programming CSV File Reader

Java Programming Character Frequency Count in String using for each

Java Programming Finds Min from Integer Array by Passing Array to Method

Java Programming Linear Search

Java Programming Binary Search

Java Programming Ternary Search

Java Programming Generates Range of Numbers

Java Programming Generates Range of Characters

Java Programming Computes Square Root Value

Java Programming Checks Number is Positive or Negative

Java Programming Checks Number is Odd or Even

Java Programming Computes Plot Area

Java Programming Convert Number of Days into Years

Java Programming Checks a Year is Leap Year, Century Year or Not

Java Programming Checks a Character is Digit, Letter or neither digit nor letter

Java Programming Checks a Number is Palindrome or not

Java Programming Sum of Two Matrix

Java Programming Power Computation

Java Programming Checks Number is an Armstrong Number or not

Java Programming Temperature Unit Conversions

Java Programming Generates Random Numbers in Specified Range

Java Programming Computes Sum of Digits and Product of Digits

Java Programming Computes Reverse Number

Java Programming Computes Factorial Value

Java Programming Checks Prime Number or not

Java Programming Computes Harmonic Series

Java Programming Generates Floyd's Triangle

Java Programming Reverses String

Java Programming Checks Palindrome String or not

Java Programming Opens Notepad

Java Programming Searches String using RegEx Pattern

Java Programming Searches Word in String

Java Programming Gets System Environment Variables

Java Programming Gets IP Address of Server Name

Java Programming Arrays Sort and Reverse using sort method

Java Programming Bubble Sorting

Java Programming Selection Sorting

Java Programming Insertion Sorting

Java Programming Merge Sorting

Java Programming Quick Sorting

Java Programming Counting Sort

Java Programming Radix Sorting

Java Programming Sorting Array of Strings

Java Programming String Characters Sorting

Java Programming Sum of First N Numbers

Java Programming Product of First N Numbers

Privacy Policy  |  Copyright@2017 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Free online programming tutorials

Email Facebook Google LinkedIn Twitter
^