Java List

  1. Create & Insert data into a List in Java
  2. Insert a data in particular index in Java
  3. Delete a data in particular index in Java
  4. Update a data in particular index in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Create & Insert data into a List in Java


List_Ex1.java

import java.awt.List;
import java.util.Scanner;
 
public class List_Ex1 {
 
	List list = new List();
	Scanner scan = new Scanner(System.in);
	
	int n;
	
	void getVal() {
		
		System.out.println("List - Create & Insert");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the data :");
		
		for(int i=0; i<n; i++) {
			
			list.add(scan.nextLine());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe Stored List data are :");
		
		for(int i=0; i<n; i++) {
			
			System.out.println(list.getItem(i));
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		List_Ex1 obj = new List_Ex1();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

List - Create & Insert

Enter 'n' value :
3
Enter the data :
Balaji
SampleCodez
Java

The Stored List data are :
Balaji
SampleCodez
Java




B. Insert a data in particular index in Java


List_Ex2.java

import java.awt.List;
import java.util.Scanner;
 
public class List_Ex2 {
 
	List list = new List();
	Scanner scan = new Scanner(System.in);
	
	int n;
	
	void getVal() {
		
		System.out.println("List - Insert a Data");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the data :");
		
		for(int i=0; i<n; i++) {
			
			list.add(scan.nextLine());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe Stored List data are :");
		
		for(int i=0; i<list.getItemCount(); i++) {
			
			System.out.println(list.getItem(i));
		}
	}
	
	void addMore() {
		
		System.out.println("\nEnter the index :");
		int i = scan.nextInt();
		
		System.out.println("Enter the data :");
		String item = scan.next();
		
		list.add(item, i-1);
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		List_Ex2 obj = new List_Ex2();
		
		obj.getVal();
		obj.display();
		
		obj.addMore();
	}
}

Sample Output

List - Insert a Data

Enter 'n' value :
2
Enter the data :
Boopathy
Deepan

The Stored List data are :
Boopathy
Deepan

Enter the index :
3
Enter the data :
Selva

The Stored List data are :
Boopathy
Deepan
Selva




C. Delete a data in particular index in Java


List_Ex3.java

import java.awt.List;
import java.util.Scanner;
 
public class List_Ex3 {
 
	List list = new List();
	Scanner scan = new Scanner(System.in);
	
	int n;
	
	void getVal() {
		
		System.out.println("List - Delete a Data");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the data :");
		
		for(int i=0; i<n; i++) {
			
			list.add(scan.nextLine());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe Stored List data are :");
		
		for(int i=0; i<list.getItemCount(); i++) {
			
			System.out.println(list.getItem(i));
		}
	}
	
	void delete() {
		
		System.out.println("\nEnter the index :");
		int i = scan.nextInt();
		
		list.remove(i-1);
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		List_Ex3 obj = new List_Ex3();
		
		obj.getVal();
		obj.display();
		
		obj.delete();
	}
}

Sample Output

List - Delete a Data

Enter 'n' value :
3
Enter the data :
JP
Ponnaiah
Naveen

The Stored List data are :
JP
Ponnaiah
Naveen

Enter the index :
2

The Stored List data are :
JP
Naveen




D. Update a data in particular index in Java


List_Ex4.java

import java.awt.List;
import java.util.Scanner;
 
public class List_Ex4 {
 
	List list = new List();
	Scanner scan = new Scanner(System.in);
	
	int n;
	
	void getVal() {
		
		System.out.println("List - Update a Data");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the data :");
		
		for(int i=0; i<n; i++) {
			
			list.add(scan.nextLine());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe Stored List data are :");
		
		for(int i=0; i<list.getItemCount(); i++) {
			
			System.out.println(list.getItem(i));
		}
	}
	
	void update() {
		
		System.out.println("\nEnter the index :");
		int i = scan.nextInt();
		
		System.out.println("Enter the data :");
		String item = scan.next();
		
		list.replaceItem(item, i-1);
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		List_Ex4 obj = new List_Ex4();
		
		obj.getVal();
		obj.display();
		
		obj.update();
	}
}

Sample Output

List - Update a Data

Enter 'n' value :
3
Enter the data :
Arul
Ashok
Karthik

The Stored List data are :
Arul
Ashok
Karthik

Enter the index :
2
Enter the data :
MilkMan

The Stored List data are :
Arul
MilkMan
Karthik






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com