Here we learn what is ArrayList In Java.But before that we must know, What is Collection Interface in Java ?
Class ArrayList<E>
All Implemented Interfaces:
Direct Known Subclasses:
AttributeList, RoleList, RoleUnresolvedList
Declaration of ArrayList:
public class ArrayList<E>
extends AbstractList<E>
ArrayList is an Resizable array implementation of List interface.
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. As elements are added to an ArrayList, its capacity grows automatically.
An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation.
ArrayList is an example of fail-fast iterator and ArrayList implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.To prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new ArrayList(...));
This class is a member of the Java Collections Framework.
Constructor Summary of ArrayList class:
Constructor
|
Description
|
ArrayList()
|
Constructs an empty list with an initial capacity
of ten.
|
ArrayList(Collection<? extends E> c)
|
Constructs a list containing the elements of the
specified collection, in the order they are returned by the collection's
iterator.
|
ArrayList(int initialCapacity)
|
Constructs an empty list with the specified
initial capacity.
|
Method Summary of ArrayList class:
Modifier and Method Name
|
Description
|
boolean add(E e)
|
Appends the specified element to the end of this
list.
|
void add(int index, E element)
|
Inserts the specified element at the specified
position in this list.
|
boolean addAll(Collection<? extends E> c)
|
Appends all of the elements in the specified
collection to the end of this list, in the order that they are returned by
the specified collection's Iterator.
|
boolean addAll(int index, Collection<? extends E> c)
|
Inserts all of the elements in the specified
collection into this list, starting at the specified position.
|
void clear()
|
Removes all of the elements from this list.
|
Object clone()
|
Returns a shallow copy of this ArrayList
instance.
|
boolean contains(Object o)
|
Returns true if this list contains the specified
element.
|
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.
|
E get(int index)
|
Returns the element at the specified position in
this list.
|
int indexOf(Object o)
|
Returns the index of the first occurrence of the
specified element in this list, or -1 if this list does not contain the
element.
|
boolean isEmpty()
|
Returns true if this list contains no elements.
|
Iterator<E> iterator()
|
Returns an iterator over the elements in this
list in proper sequence.
|
int lastIndexOf(Object o)
|
Returns the index of the last occurrence of the
specified element in this list, or -1 if this list does not contain the
element.
|
ListIterator<E> listIterator()
|
Returns a list iterator over the elements in this
list (in proper sequence).
|
ListIterator<E> listIterator(int index)
|
Returns a list iterator over the elements in this
list (in proper sequence), starting at the specified position in the list.
|
E remove(int index)
|
Removes the element at the specified position in
this list.
|
boolean remove(Object o)
|
Removes the first occurrence of the specified
element from this list, if it is present.
|
boolean removeAll(Collection<?> c)
|
Removes from this list all of its elements that
are contained in the specified collection.
|
Protected void removeRange(int fromIndex, int toIndex)
|
Removes from this list all of the elements whose
index is between fromIndex, inclusive, and toIndex, exclusive.
|
boolean retainAll(Collection<?> c)
|
Retains only the elements in this list that are
contained in the specified collection.
|
E set(int index, E element)
|
Replaces the element at the specified position in
this list with the specified element.
|
int size()
|
Returns the number of elements in this list.
|
List<E> subList(int
fromIndex, int toIndex)
|
Returns a view of the portion of this list
between the specified fromIndex, inclusive, and toIndex, exclusive.
|
Object[] toArray()
|
Returns an array containing all of the elements
in this list in proper sequence (from first to last element).
|
<T> T[] toArray(T[] a)
|
Returns an array containing all of the elements
in this list in proper sequence (from first to last element); the runtime
type of the returned array is that of the specified array.
|
void trimToSize()
|
Trims the capacity of this ArrayList instance to
be the list's current size.
|
Example of ArrayList class:
import java.util.ArrayList;
public class ArrayListClassExample {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
System.out.println("Initial Size of ArrayList : "+list.size());
//Adding Elements in ArrayList
list.add("India");
list.add("japan");
list.add("Russia");
list.add("USA");
//Getting ArrayList Size
System.out.println("Size of ArrayList after Element addition : "+list.size());
System.out.println("Displaying ArrayList : "+list);
//Removing Elements from ArrayList
list.remove(0);
list.remove("USA");
System.out.println("Size of ArrayList after element deletion : "+list.size());
System.out.println("Displaying ArrayList after element deletion : "+list);
}
}
Program Output:
Initial Size of ArrayList : 0
Size of ArrayList after Element addition : 4
Displaying ArrayList : [India, japan, Russia, USA]
Size of ArrayList after element deletion : 2
Displaying ArrayList after element deletion : [japan, Russia]
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment