List Interface in Java with Examples


List (I) :

  • List is the child Interface of collection.
  • If we want to represent a group of individual objects where duplicate objects are allowed and insertion order is preserved. Then we should go for list.
  • Insertion order will be preserved by means of Index.
  • We can differentiate duplicate objects by using Index. Hence Index place a very important role in list.


List Interface methods:-

Boolean add (int index, object O)

Boolean add All (int index, collection C)

Object remove (int index)

Object get (int index)

Object set (int index, object new)

Int index of (object O)

Int lastIndexOf(object o)

List Iterator list Iterator()

 


List Contains below Classes:-

  • Array List (c)
  • Linked List (C)
  • Vector List (C )
  • Stack (C)

 


Example:-

import java.util. *;

Class ArrayListDemo

{

Public Static Void main (String[] args)

{

ArrayList a = new ArrayList()

a.add (“A”);

a.add (10);

a.add(“A”);

a.add (null);

System.out.println(a);

a.remove(2);

System.out.println(a);

a.add(“N”);

a.add(2, “M”);

a.add(“N”);

System.out.println(a);

}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *