List Concept Simplified In Java

0
20
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share


What is a List:

List is the child interface of collection.Collection interface was introduced in java 1.2 version and  List Interface got introduced in the same version. The List interface contains an ordered sequence of elements available in a collection.It allows duplicate elements in the list.The List interface inherits the methods of Collection interface.

Inside list interface there are 3 primary classes. They are..

  1. ArrayList-1.2 version
  2. AbstractSequentialList-1.2 Version
  3. Vector-1.0 version (Legacy class)

2.1 LinkedList-1.2 Version
3.1 Stack (1.0 version-legacy class)

When we should use list:

If we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order preserved then we should go for List interface.
In Java 1.2 version Vector,Stack classes are re engineered to implement List interface.

As we can preserve the insertion order hence index will be generated. We can differentiate duplicate objects by using Index. So Index plays an important role in List.

List Interface is having the following methods:


void add(int index,Object o);//adds the element 'o' in the specified index.
void add(int index,Collection c);//adds the elements of the collection c at the specified index.
boolean addAll(int index,Collection c);//adds the elements of the collection c at the specified index.
Object get(int index);//returns the element available in the specified index.
Object remove(int index);//removes the element available in the specified index.
Object set(int index,Object new);//This method is very interesting, it replaces the element present at
// specified index with provided object and returns old object]
int indexOf(Object o); //returns the index of first occurrence of o,if it does not available it returns -1
int lastIndexOf(Object o);//returns the index of last occurrence of o,if it does not available it returns -1
ListIterator listIterator();//returns a listIterator of the elements
ListIterator listIterator(int index);//returns a listIterator of the elements from the specified index.
void set(int index,Object o);//replaces the element in the specified index with specified element.
List subList(int stratIndex,int endIndex);//returns the elements available from the specified boundaries.
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here