Write a java program for creating list

Write a java program for creating list

Aim : To write a java program for creating list.

The Java.util. List is a child interface of collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.

Procedure for List creation:

  1. List Interface is the subinterface of Collection.
  2. It contains index-based methods to insert and delete elements.
  3. It is a factory of ListIterator interface.
  4. List<String> al=new ArrayList<String>();    
  5. al.add(“Amit”);    
  6. al.add(“Vijay”);    
  7. al.add(“Kumar”);    
  8. al.add(1,”Sachin”);    
  9. ListIterator<String> itr=al.listIterator();    

1.Create List Operation Program:

// Java program to demonstrate positional access

// operations on List interface

import java.util.*;

public class ListDemo


 public static void main (String[] args)    


 // Creating a list        

 List<Integer> l1 = new ArrayList<Integer>();       

  l1.add(0, 1);  // adds 1 at 0 index        

 l1.add(1, 2);  // adds 2 at 1 index         

System.out.println(l1);  // [1, 2]           

 // Creating another list         

List<Integer> l2 = new ArrayList<Integer>();        




 // Will add list l2 from 1 index       

  l1.addAll(1, l2);        


  // Removes element from index 1         


 System.out.println(l1); // [1, 2, 3, 2]            

// Prints element at index 3        


 // Replace 0th element with 5       

  l1.set(0, 5);          





Write a java program for creating list




Have any Question or Comment?

Leave a Reply

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