Java Array List Example Program
package com.practice.list;
import java.util.*;
public class ArrayListExample {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " + al.size());
// add elements to the array list
al.add("M");
al.add("A");
al.add("D");
al.add("D");
al.add("Y");
al.add(1, "A");
System.out.println("Size of al after additions: " + al.size());
// display the array list
System.out.println("Contents of al: " + al);
// Remove elements from the array list
al.remove("D");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}
Output :
Initial size of al: 0
Size of al after additions: 6
Contents of al: [M, A, A, D, D, Y]
Size of al after deletions: 4
Contents of al: [M, A, D, Y]
import java.util.*;
public class ArrayListExample {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " + al.size());
// add elements to the array list
al.add("M");
al.add("A");
al.add("D");
al.add("D");
al.add("Y");
al.add(1, "A");
System.out.println("Size of al after additions: " + al.size());
// display the array list
System.out.println("Contents of al: " + al);
// Remove elements from the array list
al.remove("D");
al.remove(2);
System.out.println("Size of al after deletions: " + al.size());
System.out.println("Contents of al: " + al);
}
}
Output :
Initial size of al: 0
Size of al after additions: 6
Contents of al: [M, A, A, D, D, Y]
Size of al after deletions: 4
Contents of al: [M, A, D, Y]
Comments
Post a Comment