ArrayList

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

Programming in Java

Topic: ArrayList
Introduction
• Standard Java arrays are of a fixed length.

• After arrays are created, they cannot grow or shrink, which


means we can not resize Arrays.

• Arrays are useful when we know in advance how many


elements the array is to hold.

• ArrayList is a collection which provides the implementation of


resizable array.
ArrayList
• The ArrayList class extends AbstractList and implements the List
interface.

• Defined in java.util package.

• ArrayList supports dynamic arrays that can grow as needed.

• Array lists are created with an initial size.

• When this size is exceeded, the collection is automatically enlarged.

• When objects are removed, the array may be shrunk.


ArrayList Constructors
• T he ArrayList class supports three constructors.

 ArrayList() : creates an empty array list with an initial capacity


sufficient to hold 10 elements.
new capacity= (current capacity * 3/2)+1
 ArrayList(int capacity) : creates an array list that has the
specified initial capacity.

 ArrayList(Collection c) : creates an array list that is initialized


with the elements of the collection c.
Methods of ArrayList
boolean add(Object element)
• Appends the specified element to the end of this list.

void add(int index, Object element)


• Inserts the specified element at the specified position index in
this list.
• Throws IndexOutOfBoundsException if the specified index is
out of range.
boolean addAll(Collection c )
• Appends all of the elements in the specified collection to the end of
this list.
• Throws NullPointerException if the specified collection is null.

boolean addAll(int index, Collection c )


• Inserts all of the elements of the specified collection into this list,
starting at the specified position.
• Throws NullPointerException if the specified collection is null.
void clear()
• Removes all of the elements from this list.

Object remove(int index)


• Removes the element at the specified position in this list.

boolean remove(Object o)
• Removes the first specified object present in the arraylist.
int size()
• Returns the number of elements in the list.
boolean contains(Object o)
• Returns true if this list contains the specified element.

Object get(int index)


• Returns the element at the specified position in this list.

int indexOf(Object o)
• Returns the index in this list of the first occurrence of the
specified element, or -1 if the List does not contain the element.

int lastIndexOf(Object o)
• Returns the index in this list of the last occurrence of the
specified element, or -1.
void ensureCapacity(int minCapacity)
• Increases the capacity of this ArrayList instance, if necessary,
to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.

Object set(int index, Object element)


• Replaces the element at the specified position in this list with
the specified element.
• Throws IndexOutOfBoundsException if the specified index is
out of range (index < 0 || index >= size()).
boolean isEmpty()
• used to check whether the Arraylist is empty or not?

void trimToSize()
• reduces the size of an arraylist to the number of elements
present in the arraylist.
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " +
al.size());
al.add("C");
al.add("A");
al.add("E");
al.add(10);
al.add("D");
al.add(34.89);
al.add(1, "A2");
System.out.println("Size of al after additions: " +
al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al);
}
}

You might also like