ArrayList Concept Simplified in Java



Arraylist is an advanced data structure that can be stretched to accommodate additional elements. When we do not have enough elements in the Arraylist, then Arraylist can be shrinked to lower size. It can hold dynamic behavior of the data. So a dynamic array can be put inside ArrayList. We can also change the size of the ArrayList during run time.Every instance of an ArrayList class is allowed to store a set of elements in the list.The capacity of the ArrayList increase automatically as and when the we add elements to the ArrayList. ArrayList extends to AbstractList class and implements the interface such as List,Cloneable and Serializable.

Deceleration of the ArrayList:

ArrayList variableName=new ArrayList(); 
 
The constructor of ArrayList class is given below:

public ArrayList();
//creates an empty ArrayList. the initial capacity is 10
public ArrayList(Collection c);
//creates an ArrayList with the elements specified in the Collection c passed as an argument.
public ArrayList(int Capacity);
//creates an ArrayList with the capacity specified in the argument.
 
The ArrayList class inherits the methods from List interface.The elements of an ArrayList can be directly accessed using get() and set() methods.
Number Method Name Method Description Return Comment
1 add(Object obj) Adds element to an arraylist after the last element or at the current position Object obj If the remaining size is well within permissiable limit, arraylist will not grow else it will follow an growing algorithm and increases it size
2 remove(Object obj) removes the obejct from the arraylist or from an index. Object obj Size is shrinken
3 int size() Returns the size of the arraylist. int size
4 boolean Contains(Object obj) Returns true if the obj is present inside arralist. or else it will return false boolean-true or false
Here is an example of ArrayList::

import java.util.*;
public Class ArryListTest{
public static void main(String[] args){
ArrayList myArrayList=new ArrayList();
//adds "D" to the myArrayList
myArrayList.add("D");
//adds "E" to the myArrayList
myArrayList.add("E");
//adds "M" to the myArrayList
myArrayList.add("M");
//adds "O" to the myArrayList
myArrayList.add("O");
//find the size of myArrayList
int size=myArrayList.size();
System.out.println("Size of the ArrayList is "+size);
//prints the elements of myArrayList
System.out.println(myArrayList);
//removes an element from myArrayList from index-2
Object val=myArrayList.remove(2);
//prints the elements of that is removed
System.out.println("Removed Element-"+val.toString());
//prints the elements of myArrayList
System.out.println(myArrayList);
//checks if the myArrayList contains an element "Z"
boolean flag1=myArrayList.contains("Z");
System.out.println(flag1);
//checks if the myArrayList contains an element "O"
boolean flag2=myArrayList.contains("O");
System.out.println(flag2);
The output of the program will be as bellow:
$javac ArrayListTest.java
Note: ArrayListTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$java -Xmx128M -Xms16M ArrayListTest
Size of the ArrayList is 4
[D, E, M, O]
Removed Element-M
[D, E, O]
false
true
Use of Generics:
from java 5.0 on words each collection class is generalized by providing the type inside the angular bracket(<>). Hence the new definition of ArrayList is given as :

ArrayList<type> variableName=new ArrayList<type>();
example
ArrayList<String> name=new ArrayList<string>(); 
In the previous definition ,an user can enter almost anything into an ArrayList but with the use of generics this issue is rectified and the compiler will throw an error if we try to enter different type into another type of ArrayList. With the new definition java knows what kind of values the ArrayList may contain. While fetching an ArrayList, we can also predict what is coming out of an ArrayList.
Example:

ArrayList<String> name=new ArrayList<String>(); 
name.add(new Room());
//compiler will throw an error.
name.add(new String("Test"));
//it is fine as the element is of type String
 

ArrayList Concept Simplified in Java ArrayList Concept Simplified in Java Reviewed by Animesh Chatterjee on August 27, 2018 Rating: 5

No comments:

Powered by Blogger.