The List interface extends Collection and declares the behaviour of a collection that stores a sequence of elements.

·     Elements can be inserted or accessed by their position in the list, using a zero-based index.

·     A list may contain duplicate elements.

·     In addition to the methods defined by Collection.

·     Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another. 

 We can define List data type which data type will be store in the list.

I.e.

List<String> myList=new ArrayList( );
// store only String type value
myList.add(" Data1");
myList.add(" Data2");
myList.add(" Data3");
or
List mList=new ArrayList( );
// store any type value
 
mList.add(" Data1");
mList.add(" Data2");
mList.add(" Data3");
 

In List class we can add several types of data but List<e> interface we can add only e type data for example

List mList=new ArrayList( );
mList.add(" Data1");
mList.add(123);
mList.add('C');
mList.add(3.63);

 

Insert value in list

Syntax -: listObj.add(Value);

  myList.add("MyData"); 

Update value in particular index

Syntax -: listObj.set(int index, Value;

 myList.set(4, "Update"); 

Find value from index

Syntax-: listobj.get(int index);

 myList.get(2); 
Remove value from index

Syntax-: listobj.remove(int index);
 myList.remove(2); 

Get index of Object

Syntax-: listobj.indexOf(Object);
int index=myList.indexOf("Data2");
If record found then return index position otherwise return -1. 

Here I am creating example if List sample for insert, manipulation, deletion etc.

 

import java.util.ArrayList;

import java.util.List;

 

public class Demo {

     static List<String> myList;

 

     public static void main(String args[]) {

 

           myList = new ArrayList<String>();

           // insertData("Data1");

 

           for (int i = 1; i < 11; i++) {

                insertData("Item" + i);

 

           }

           updateList("Data10", 10);

           DeleteByData("Data10");

           DeleteByIndex(9);

           System.out.print(myList.toString());

 

     }

 

     public static void insertData(String data) {

           myList.add(data);

           // System.out.println("Data Inserted.");

 

     }

 

     public static void updateList(String data, int index) {

           index = index - 1;

           if (index < myList.size()) {

                myList.set(index, data);

                System.out.println("Data Updated.");

           } else

    System.out.println("You have not enough data index for update.");

     }

 

     public static void DeleteByIndex(int index) {

           index = index - 1;

           if (index < myList.size()) {

                myList.remove(index);

                System.out.println("Data Deleted.");

           } else

System.out.println("You have not enough data index for delete.");

 

     }

 

     public static void DeleteByData(String data) {

           int index = myList.indexOf(data);

           if (index >= 0) {

                myList.remove(index);

                System.out.println("Data Deleted.");

           } else

                System.out.println("Data not found for delete.");

     }

}

 

 

Output

 

 

  Modified On Dec-05-2017 12:59:55 AM

Leave Comment