JAVA

Java ArrayList class

Java ArrayList class uses a dynamic array for storing the elements. It inherits AbstractList class and implements List interface.

The important points about Java ArrayList class are:

·        Java ArrayList class can contain duplicate elements.

·        Java ArrayList class maintains insertion order.

·        Java ArrayList class is non synchronized.

·        Java ArrayList allows random access because array works at the index basis.

·        In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

Methods of Java ArrayList

 

Method

Description

void add(int index, Object element)

It is used to insert the specified

element at the specified position index in a list.

boolean addAll(Collection c)

It is used to append 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.

void clear()

It is used to remove all of the elements from this list.

 

ArrayList example program:

public class ArrayList{

public static void main(String args[]){

ArrayList list=new ArrayList();

list.add("Sindhu");

list.add("Deepthi");

list.add("Chandrakala");

list.add("Sai Charan");

list.add("Maithili");

list.add("Deepya");

list.add("Jeeshitha");

list.add("Sunitha");

Iterator itr=list.iterator();

while(itr.hasNext()){

 

System.out.println(itr.next());

}

}}


OUTPUT:

Sindhu

Deepthi

Chandrakala

Sai Charan

Maithili

Deepya

Jeeshitha

Sunitha


 

Java LinkedList class

Java LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.

·        The important points about Java LinkedList are:

·        Java LinkedList class can contain duplicate elements.

·        Java LinkedList class maintains insertion order.

·        Java LinkedList class is non synchronized.

·        In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.

Methods of Java LinkedList

 

Method

Description

void add(int index, Object element)

It is used to insert the specified element at the specified position index in a list.

void addFirst(Object o)

It is used to insert the given element at the beginning of a list.

void addLast(Object o)

It is used to append the given element to the end of a list.

int size()

It is used to return the number of elements in a list

boolean add(Object o)

It is used to append the specified element to the end of a list.

 

LinkedList example program:

public class LinkedList{

public static void main(String args[]){

ArrayList list=new LinkedList();

list.add("Sindhu");

list.add("Deepthi");

list.add("Chandrakala");

list.add("Sai Charan");

list.add("Maithili");

list.add("Deepya");

list.add("Jeeshitha");

list.add("Sunitha");

Iterator itr=list.iterator();

while(itr.hasNext()){

 

System.out.println(itr.next());

}

}}


OUTPUT

Sindhu

Deepthi

Chandrakala

Sai Charan

Maithili

Deepya

Jeeshitha

Sunitha