3. Arraylist ( Dynamic Array)
3. Arraylist ( Dynamic Array)
ArrayList Constructors :
ArrayList Methods :
Method Description
void add(int index, E It inserts the specified element at the
element) specified position in a list.
boolean add(E e) It appends the specified element at the end
of a list.
boolean addAll(Collect It appends all of the elements in the specified
ion<? extends E> c) collection to the end of this list.
boolean addAll(int It appends all the elements in the specified
index, Collection<? collection, starting at the specified position of
extends E> c) the list.
void clear() It removes all of the elements from this list.
void ensureCapacity(in It enhances the capacity of
t requiredCapacity) an ArrayList instance.
E get(int index) It fetches the element from the particular
position of the list.
boolean isEmpty() It returns true if the list is empty, otherwise
false.
int lastIndexOf(Object It returns the index in this list of the last
o) occurrence of the specified element, or -1 if
the list does not contain this element.
Object[] toArray() It returns an array containing all of the
elements in this list in the correct order.
<T> T[] toArray(T[] a) It returns an array containing all of the
elements in this list in the correct order.
Object clone() It returns a shallow copy of an ArrayList.
boolean contains(Obje It returns true if the list contains the specified
ct o) element
int indexOf(Object o) It returns the index in this list of the first
occurrence of the specified element, or -1 if
the List does not contain this element.
E remove(int index) It removes the element present at the
specified position in the list.
boolean remove(Objec It removes the first occurrence of the
t o) specified element.
boolean removeAll(Co It removes all the elements from the list.
llection<?> c)
boolean removeIf(Pre It removes all the elements from the list that
dicate<? super E> satisfies the given predicate.
filter)
protected It removes all the elements lies within the
void removeRange(int given range.
fromIndex,
int toIndex)
void replaceAll(Unary It replaces all the elements from the list with
Operator<E> the specified element.
operator)
void trimToSize() It trims the capacity of this ArrayList instance
to be the list's current size.
Implementation Of All Methods Of ArrayList In Single Example :
// Creating an ArrayList :
ArrayList<String> Fruits = new ArrayList<>
();
// Displaying ArrayList :
System.out.println("Declared ArrayList :
"+Fruits);
System.out.println("");
/*
1- Add Elements to ArrayList
To add elements into ArrayList, we
are using add() method. It adds
elements into the list in the insertion
order.
*/
Fruits.add("Mango");
Fruits.add("Apple");
Fruits.add("Berry");
Fruits.add("Banana");
Fruits.add("Pine-Apple");
Fruits.add("Papaya");
Fruits.add("Banana");
Fruits.add("Chikoo");
System.out.println("After Adding Elements :
" +Fruits);
System.out.println("");
/*
2- Removing Elements
To remove elements from the list,
we are using remove(obj) method that
remove the specified elements. We can
also pass index value to remove the
elements of it.
*/
Fruits.remove("Berry");
Fruits.remove(5);
System.out.println("After Deleting Elements :
"+Fruits);
System.out.println("");
/*
3- Traversing Elements of ArrayList
Since ArrayList is a collection then
we can use loop to iterate its elements.
In this example we are traversing
elements.
*/
System.out.println("After Traversing the
Elements :");
for(String element : Fruits)
{
System.out.println(element);
}
System.out.println("");
/*
4- Get size of ArrayList
Sometimes we want to know number
of elements an ArrayList holds. In
that case we use size() then returns
size of ArrayList which is equal to
number of elements present in the list.
*/
System.out.println("Total Elements :
"+Fruits.size());
System.out.println("");
/*
5- Sorting ArrayList Elements
To sort elements of an ArrayList,
Java provides a class Collections
that includes a static method sort().
In this example, we are using sort
method to sort the elements.
*/
Collections.sort(Fruits);
// By default Sort in Ascending ...
System.out.println("After Sorting ELements :
"+Fruits);
System.out.println("");
/*
6- add(index,obj)
Add Element at particular index
It inserts the specified element at
the specified position in a list.
*/
Fruits.add(2, "Berry");
Fruits.add(5, "Papaya");
System.out.println(Fruits);
System.out.println("");
/*
7- IsEmpty() Method
It returns true if the list is
empty, otherwise false.
*/
boolean IE = Fruits.isEmpty();
System.out.println("Fruits ArrayList is Empty
: "+IE);
System.out.println("");
/*
8- get() Method
It fetches the element from
the particular position of the list.
*/
System.out.println("");
/*
9- ensureCapacity(int requiredCapacity)
It enhances the capacity of
an ArrayList instance.
*/
Fruits.ensureCapacity(10);
System.out.println("");
/*
10- int indexOf(Object o)
It returns the index in this list of
the first occurrence of the specified
element, or -1 if the List does not
contain this element.
*/
System.out.println("First Index of Papaya in
the list : "+Fruits.indexOf("Papaya"));
System.out.println("");
/*
11- int lastIndexOf(Object o)
It returns the index in this list of
the last occurrence of the
specified element, or -1 if the list
does not contain this element.
*/
System.out.println("");
/*
12- toArray()
It returns an array containing all
of the elements in this list in the
correct order.
Syntax:
public Object[] toArray()
or
public <T> T[] toArray(T[] a)
*/
System.out.println("");
/*
13- Object clone()
It returns a shallow copy of
an ArrayList
*/
System.out.println("");
/*
14- contains(Object o)
It returns true if the list contains
the specified element
*/
System.out.println("");
/*
15- removeAll(Collection<?> c)
It removes all the elements from
the list.
*/
System.out.println("");
/*
16- removeIf(Predicate<? super E> filter)
It removes all the elements from
the list that satisfies the given
predicate.
*/
System.out.println("");
/*
17- addAll(Collection<? extends E> c)
It appends all of the elements in
the specified collection to the end
of this list.
*/
System.out.println("");
/*
18-addAll(int index, Collection<? extends E>
c)
It appends all the elements in the
specified collection, starting at
the specified position of the list.
*/
System.out.println("addAll from Fruits to
again in FruitsNew : "+FruitsNew.addAll(3, Fruits));
System.out.println("Added Element from Fruits
: "+FruitsNew);
System.out.println("");
/*
19- replaceAll(UnaryOperator<E> operator)
It replaces all the elements from
the list with the specified element.
*/
FruitsNew.replaceAll(e->e.toUpperCase());
System.out.println("Replace all
From FruitsNew : "+FruitsNew);
}
}