Skip to main content

Command Palette

Search for a command to run...

ArrayList

Published
•2 min read•View as Markdown
F

Assalamualaikum warahmatullah wabarakatuh( traditional Islamic greeting in Arabic "Assalamu alaikum": "Peace be upon you." "Wa rahmatullahi": "And the mercy of Allah." "Wa barakatuh": "And His blessings.") I’m Faraz Alam, and I’m documenting my journey through the world of software technology. Despite earning a master’s degree in Computer Applications and having access to opportunities provided by my tier-3 college, I struggled to take full advantage of them due to poor management and a less productive environment. This led to joblessness, primarily due to a lack of upskilling. Now, I am dedicated to enhancing my skills and knowledge with the aim of securing a valuable job offer from leading product-based companies, including those in the FAANG group (Facebook, Amazon, Apple, Netflix, Google) and other prominent tech giants. This documentation is not for self-promotion; rather, it is for anyone who is waiting for an opportunity but feels they lack the tools and skills required to overcome challenges. It’s a testament to the effort and responsibility needed to navigate the journey towards success when you take charge of your own path. Date: 31 July 2024, 07:25 AM This page will be updated regularly to reflect new achievements and milestones as I continue to build my career.

  1. Write a program to perform different operation on array list.

     import java.util.*;
     public class Main {
         public static void main(String[] args) {
             //Declaration of ArrayList
             ArrayList<Integer> list1= new ArrayList<>();
             //inserting element in ArrayList
             list1.add(1);
             list1.add(2);
             list1.add(3);
             list1.add(4);
             //get operation
             int element= list1.get(2);
             System.out.println(element);
             //traversal of array
             for(int i=0; i<list1.size();i++){
                 System.out.print(list1.get(i)+" ");
             }
             //removing element
             list1.remove(2);
             System.out.println(list1);
             //adding element at particular index
             list1.add(2,3);
             System.out.println(list1);
             //getting size of array list
             System.out.println(list1.size());  
         }
     }
    
  2. Write a program to find smallest and greatest in ArrayList.

     import java.util.*;
     public class Main {
         public static void main(String[] args) {
             ArrayList<Integer> list1= new ArrayList<>();
             int greatest= Integer.MIN_VALUE;
             int smallest= Integer.MAX_VALUE;
             list1.add(12);
             list1.add(17);
             list1.add(4);
             list1.add(8);
             list1.add(9);
             for(int i=0; i<list1.size();i++){
                 if(list1.get(i)>greatest){
                     greatest= list1.get(i);
                 }
                 if(list1.get(i)<smallest){
                     smallest= list1.get(i);
                 }
             }
             System.out.println(greatest+" "+ smallest);//17 4
         }
     }
    
  3. Write a program to reverse elements of array list.

     import java.util.ArrayList;
     public class Main
     {
         public static void main(String[] args) {
             ArrayList<Integer> list1=new ArrayList<>();
             list1.add(1);
             list1.add(2);
             list1.add(3);
             list1.add(4);
             list1.add(5);
             for(int i= list1.size()-1; i>=0;i--){
                 System.out.print(list1.get(i)+" ");
             }
             System.out.println();
         }
     }
    
  4. Write a program to swap two element at particular index in array list.

     import java.util.*;
     public class Main {
         public static void swap(ArrayList<Integer> list, int index1, int index2){
             int temp= list.get(index1);
             list.set(index1,list.get(index2));
             list.set(index2,temp);
         }
         public static void main(String[] args) {
             ArrayList<Integer> list1= new ArrayList<>();
             list1.add(1);
             list1.add(2);
             list1.add(3);
             list1.add(4);
             list1.add(5);
             int index1= 1;
             int index2=3;
             System.out.println(list1);//[1,2,3,4,5]
             swap(list1, index1, index2);
             System.out.println(list1);//[1,4,3,2,5]
         }
     }
    
  5. Write a program to sort elements of array list.

     import java.util.*;
     public class Main {
         public static void main(String[] args) {
             ArrayList<Integer> list1= new ArrayList<>();
             list1.add(111);
             list1.add(323);
             list1.add(134);
             list1.add(523);
             list1.add(123);
             System.out.println(list1);
             Collections.sort(list1);
             System.out.println("Ascending Order:- "+ list1);
             Collections.sort(list1, Collections.reverseOrder());
             System.out.println("Descending Order:- "+ list1);
         }
     }
    
  6. Write a program to create a multidimensional array list

     import java.util.*;
     public class Main {
         public static void main(String[] args) {
             ArrayList<ArrayList<Integer>> mainList= new ArrayList<>();
             ArrayList<Integer> list1= new ArrayList<>();
             ArrayList<Integer> list2 = new ArrayList<>();
             ArrayList<Integer> list3= new ArrayList<>();
             for(int i=1; i<=5;i++){
                 list1.add(i);
                 list1.add(i*2);
                 list1.add(i*3);
             }
             mainList.add(list1);
             mainList.add(list2);
             mainList.add(list3);
             for(int i=0; i<mainList.size();i++){
                 ArrayList<Integer> currentList= mainList.get(i);
                 for(int j=0; j<currentList.size();j++){
                     System.out.println(currentList.get(j)+" ");
                 }
                 System.out.println();
             }
         }
     }