List Interface:( Array List, Linked List, Vector, Stack)
List interface extends collection interface and It is
available in “java.util” package.
List is an ordered collection it maintains the insertion
order.
List provides control over the position where you can insert
the elements.
You can access elements by their index and search elements
in the list.
List allows you to add duplicate elements.
List allows you to have null elements.
List indexes start from 0, just like arrays
Array List:
Array List Is part of collection framework and it is
available in “java.util” package .
Array List implements List, Random Access, Cloneable,
Serializable interfaces and extends Abstract List class.
Array List is maintained insertion order and you can add and
remove element in middle.
Java Array List class can contain duplicate elements.
Java collection framework was non-generic before JDK 1.5.
Since 1.5, it is generic.
ArrayList list=new ArrayList();//creating old non-generic
arraylist
ArrayList<String> list=new ArrayList<String>();//creating
new generic arraylist
You can’t create an ArrayList of primitive types like int,
char etc.
Instead of primitive type need to use boxed types i.e., Integer,
Character, Boolean etc.
Array List default initial capacity 10.
Array List is resized by 50% when it exceeds current default
size.
Manipulation with Array List is slow because if any element
removed from the Array List all the elements are shifted in memory.
Array List consumes less memory then Linked List because Linked
List uses doubly linked list contains collection of nodes. each node consists
of 3 parts 1) previous node 2) data 3) next node.
Java Array List class is non synchronized, you must
explicitly synchronize access to an Array List if multiple threads are goanna
modify it.
Array List is better for retrieving data.
Creation of Array List:
Syntax:
List<E> al = new ArrayList<E>();
List<E> al = new ArrayList <E>(int initialcapacity);
Here, E represents element data type
Methods of Array List:
Boolean add(Element obj): this method is used to place the specified element to the end of List.
Void add(int position, Element obj): this method is used to insert and element at the specified position.
Boolean remove(Element obj): this method is used to remove the 1st occurrence of the specified element.
Element remove(int position): this method is used to remove and Element from the specified position.
Void clear(): this method remove all the elements available in the Array List.
Int Size(): this method will return the count of the no.of elements available in the Array List.
Boolean contains(element obj): this method returns true if the specified element is available in the Array List.
Element get(int position): this method is used to access an Element that is available in the specified position.
Element set(int position, Element obj): this method is used replace an element in the specified position with the specified element.
Object clone(): this method is used to return a shallow copy of an ArrayList.
Java Code:
No comments:
Post a Comment