Java ArrayList

  1. Create an Integer ArrayList in Java
  2. Create a Float ArrayList in Java
  3. Create a Double ArrayList in Java
  4. Create a Boolean ArrayList in Java
  5. Create a String ArrayList in Java
  6. Insert a data into ArrayList in Java
  7. Delete a data in ArrayList in Java
  8. Delete All data in ArrayList in Java
  9. Create a SubList from an ArrayList 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 an Integer ArrayList in Java


ArrayList_Ex1.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex1 {
 
	ArrayList<Integer> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<Integer>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (Integer) - 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.nextInt());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex1 obj = new ArrayList_Ex1();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

ArrayList (Integer) - Create & Insert

Enter 'n' value :
3
Enter the data :
148
152
157

The ArrayList
148
152
157




B. Create a Float ArrayList in Java


ArrayList_Ex2.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex2 {
 
	ArrayList<Float> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<Float>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (Float) - 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.nextFloat());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex2 obj = new ArrayList_Ex2();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

ArrayList (Float) - Create & Insert

Enter 'n' value :
3
Enter the data :
12.34
34.56
56.78

The ArrayList
12.34
34.56
56.78




C. Create a Double ArrayList in Java


ArrayList_Ex3.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex3 {
 
	ArrayList<Double> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<Double>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (Double) - 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.nextDouble());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex3 obj = new ArrayList_Ex3();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

ArrayList (Double) - Create & Insert

Enter 'n' value :
3
Enter the data :
1.123456789
2.123456789
3.123456789

The ArrayList
1.123456789
2.123456789
3.123456789




D. Create a Boolean ArrayList in Java


ArrayList_Ex4.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex4 {
 
	ArrayList<Boolean> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<Boolean>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (Boolean) - 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.nextBoolean());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex4 obj = new ArrayList_Ex4();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

ArrayList (Boolean) - Create & Insert

Enter 'n' value :
3
Enter the data :
true
false
TruE

The ArrayList
true
false
true




E. Create a String ArrayList in Java


ArrayList_Ex5.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex5 {
 
	ArrayList<String> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<String>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (String) - 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.next());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex5 obj = new ArrayList_Ex5();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

ArrayList (String) - Create & Insert

Enter 'n' value :
3
Enter the data :
Boopathy
JP
Selva

The ArrayList
Boopathy
JP
Selva




F. Insert a data into ArrayList in Java


ArrayList_Ex6.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex6 {
 
	ArrayList<String> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<String>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (String) - 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.next());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
	
	void insert() {
		
		System.out.println("\nEnter the index :");
		int i = scan.nextInt();
		
		System.out.println("Enter the data :");
		String item = scan.next();
		
		list.add(i-1, item);
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex6 obj = new ArrayList_Ex6();
		
		obj.getVal();
		obj.display();
		
		obj.insert();
	}
}

Sample Output

ArrayList (String) - Insert a data

Enter 'n' value :
3
Enter the data :
aaa
bbb
ccc

The ArrayList
aaa
bbb
ccc

Enter the index :
2
Enter the data :
xxx

The ArrayList
aaa
xxx
bbb
ccc




G. Delete a data in ArrayList in Java


ArrayList_Ex7.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex7 {
 
	ArrayList<String> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<String>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (String) - 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.next());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(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[]) {
		
		ArrayList_Ex7 obj = new ArrayList_Ex7();
		
		obj.getVal();
		obj.display();
		
		obj.delete();
	}
}

Sample Output

ArrayList (String) - Delete a data

Enter 'n' value :
3
Enter the data :
aaa
bbb
ccc

The ArrayList
aaa
bbb
ccc

Enter the index :
1

The ArrayList
bbb
ccc




H. Delete All data in ArrayList in Java


ArrayList_Ex8.java

import java.util.ArrayList;
import java.util.Scanner;
 
public class ArrayList_Ex8 {
 
	ArrayList<String> list;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<String>();
		scan = new Scanner(System.in);
		
		System.out.println("ArrayList (String) - Delete All");
		
		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.next());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
	
	void delete() {
		
		System.out.println("\nDo you want to delete all ? (y/n)");
		String result = scan.next(); 
 
		if(result.equalsIgnoreCase("y"))
			list.clear();
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex8 obj = new ArrayList_Ex8();
		
		obj.getVal();
		obj.display();
		
		obj.delete();
	}
}

Sample Output

ArrayList (String) - Delete All

Enter 'n' value :
3
Enter the data :
aaa
bbb
ccc

The ArrayList
aaa
bbb
ccc

Do you want to delete all ? (y/n)
y

The ArrayList
ArrayList is Empty..




I. Create a SubList from an ArrayList in Java


ArrayList_Ex9.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class ArrayList_Ex9 {
 
	ArrayList<String> list;
	List<String> sublist;
	Scanner scan;
	
	int n;
	
	void getVal() {
		
		list =  new ArrayList<String>();
		sublist = new ArrayList<String>();
		scan = new Scanner(System.in);
		
		System.out.println("Create a SubList from an ArrayList");
		
		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.next());
		}		
	}
	
	void display() {
		
		System.out.println("\nThe ArrayList");
		
		if(list.isEmpty()) {
			
			System.out.println("ArrayList is Empty..");
		}		
		else {
			
			for(int i=0; i<list.size(); i++) {
				
				System.out.println(list.get(i));
			}
		}
	}
	
	void sublist() {
		
		System.out.println("\nStarting index :");
		int start = scan.nextInt();
		
		System.out.println("Ending index :");
		int end = scan.nextInt();
		
		sublist = list.subList(start-1, end);
		
		display();
		
		System.out.println("\nThe SubList");
		
		for(int i=0; i<sublist.size(); i++) {
			
			System.out.println(sublist.get(i));
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		ArrayList_Ex9 obj = new ArrayList_Ex9();
		
		obj.getVal();
		obj.display();
		
		obj.sublist();
	}
}

Sample Output

Create a SubList from an ArrayList

Enter 'n' value :
5
Enter the data :
1
2
3
4
5

The ArrayList
1
2
3
4
5

Starting index :
2
Ending index :
4

The ArrayList
1
2
3
4
5

The SubList
2
3
4






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com