0% found this document useful (0 votes)
2 views13 pages

3. Arraylist ( Dynamic Array)

The document provides an overview of the ArrayList class in Java, detailing its implementation as a dynamic array that supports duplicate elements and maintains insertion order. It outlines the constructors, methods for manipulating elements, and includes examples of adding, removing, and accessing elements within an ArrayList. Additionally, it highlights the performance characteristics and limitations of ArrayLists, such as their lack of synchronization and slower manipulation due to element shifting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views13 pages

3. Arraylist ( Dynamic Array)

The document provides an overview of the ArrayList class in Java, detailing its implementation as a dynamic array that supports duplicate elements and maintains insertion order. It outlines the constructors, methods for manipulating elements, and includes examples of adding, removing, and accessing elements within an ArrayList. Additionally, it highlights the performance characteristics and limitations of ArrayLists, such as their lack of synchronization and slower manipulation due to element shifting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ARRAYLIST ( DYNAMIC ARRAY )

09 February 2024 14:28

This class provides implementation of an array based data structure


that is used to store elements in linear order. This class implements
List interface and an abstract AbstractList class. It creates a dynamic
array that grows based on the elements strength.

Java ArrayList class Declaration :

public class ArrayList<E> extends AbstractList<E> implements List<E>,


RandomAccess, Cloneable, Serializable { }

1. ArrayList supports dynamic array that can grow as needed.


2. It can contain Duplicate elements and it also maintains the
insertion order.
3. Manipulation is slow because a lot of shifting needs to be occurred
if any element is removed from the array list.
4. ArrayLists are not synchronized.
5. ArrayList allows random access because it works on the index
basis.

ArrayList Constructors :

ArrayList class has three constructors that can be used to


create Arraylist either from empty or elements of other collection.

• ArrayList() - It creates an empty ArrayList


• ArrayList( Collection C ) - It creates an ArrayList that is initialized
with elements of the Collection C
• ArrayList( int capacity ) - It creates an ArrayList that has the
specified initial capacity

Example: Creating an ArrayList :

Lets create an ArrayList to store string elements. Here list is empty


because we did not add elements to it.
Output : []

ArrayList Methods :

The below table contains methods of Arraylist. We can use them to


manipulate its elements.

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 :

public class Implement_ArrayList


{
public static void main(String[] args)
{

// 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("By Using Get Method from


Index We access Elements as :");
for( int i = 0 ; i < Fruits.size() ; i++ )
{
System.out.println(Fruits.get(i));
}

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("Last Index of Papaya


in the list :
"+Fruits.lastIndexOf("Papaya"));

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)
*/

Object[] arr = Fruits.toArray();


System.out.println("Element of ArrayList as
Array : "+Arrays.toString(arr));

System.out.println("");

/*
13- Object clone()
It returns a shallow copy of
an ArrayList
*/

Object Fruits1 = Fruits.clone();


System.out.println("Clone of
Fruits Arraylist in Fruits1 as : "+Fruits1);

System.out.println("");

/*
14- contains(Object o)
It returns true if the list contains
the specified element
*/

System.out.println("Fruits contain Mango :


"+Fruits.contains("Mango"));

System.out.println("");

/*
15- removeAll(Collection<?> c)
It removes all the elements from
the list.
*/

ArrayList<String> Places = new ArrayList<>


();
Places.add("Nagpur");
Places.add("Latur");
Places.add("Mumbai");
Places.add("Pune");
System.out.println("Places ArrayList :
"+Places);
System.out.println("RemoveAll From Places :
"+Places.removeAll(Places));
System.out.println("After Removing all the
element in Places Its look Like : "+Places);

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("Gives True if size of


fruits is zero and it will remove all the elements if
size is zero : "+Fruits.removeIf(n-> ( Fruits.size()
== 0)));

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.
*/

ArrayList<String> FruitsNew = new ArrayList<>


();
System.out.println("Add all in
new ArrayList FruitsNew :"+FruitsNew.addAll(Fruits));

System.out.println("Added Element from Fruits


: "+FruitsNew);

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);

}
}

You might also like