Java Program on Collection Array List Part – 1

Free Java courses with 37 real-time projects - Learn Java

Program 1

package dataflair;
import java.util.*;
public class TestMain 
{
    public static void main(String[] args) 
    {
          ArrayList<Integer>A=new ArrayList<Integer>(5);
          System.out.println(A.isEmpty());
          A.add(4);  //0
          A.add(5);  //1
         A.add(53);  //2
          A.add(17);  //3
          A.add(2);  //4
          A.add(53);  //5
          A.add(67);
          A.add(53);
          A.add(77);
          System.out.println(A);
        System.out.println("Index of "+A.indexOf(53));
        System.out.println("Last Index of "+A.lastIndexOf(53));
        List<Integer>B=new ArrayList<Integer>(5);
        B=A.subList(2,6);
         System.out.println(B);
         Object ar[];
         ar=A.toArray();
         for(Object O:ar)
             System.out.println(O); 
        //A.add(2, 125);
//        A.set(7, 125);
//        System.out.println(A);
//        System.out.println(A.get(4)); 
         
//        Integer I=2;
//        A.remove(I);
//        System.out.println(A);
        // Generic 
//        ArrayList<String>A=new ArrayList<String>(5);
//        System.out.println(A.size());
//        A.add("Rajesh");
//        System.out.println(A.add("vivek"));
//        A.add("Amit");
//        A.add("Ashok");
//        System.out.println(A.add("vivek"));
//        
//        A.add("Anurag");
//        A.add("Satish");
//        System.out.println(A.add("vivek"));
//        
//        System.out.println(A);  
//      System.out.println("-----------------------------------------");
      // Remove according to object  
      //System.out.println(A.remove("Amitesh"));
        
      // Remove according to index number  
//        System.out.println(A.remove(9));
//        System.out.println(A);
        
//        System.out.println(A.size());
//        
        //Non Generic
//         ArrayList mylist=new ArrayList();
//         mylist.add("Vivek");
//         mylist.add(140);
//         mylist.add(true);
//         mylist.add(56.77);
//         System.out.println(mylist);
    }
    
}

 

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

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