Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java I need to create two Integer arraylists containing n elements each where

ID: 644117 • Letter: I

Question

In Java I need to create two Integer arraylists containing n elements each where n will be taken from the user with a scanner.

The program should print the output of merging these two arraylists by alternating elements from both sequences.

and also print the output of merging these two arraylists by taking the first half of the first arraylist and the second half of the second arraylist.

an example of the following printed would be...

Enter the size n of the lists : 6

ArrayList 1: 1 2 4 5 8 20

ArrayList 2: 12 13 11 10 9 19

Output 1: 1 12 2 13 4 11 5 10 8 9 20 19

Output 2: 1 2 4 10 9 19

Explanation / Answer

import java.util.ArrayList;
import java.util.Scanner;

public class ArrayList_Ex1 {

   ArrayList<Integer> list;
   ArrayList<Integer> list1;
   ArrayList<Integer> list3;
   ArrayList<Integer> list4;
  
   Scanner scan;
  
   int n;
  
   void getVal() {
      
       list = new ArrayList<Integer>();
       list1 = new ArrayList<Integer>();
      
       scan = new Scanner(System.in);
      
       System.out.println("ArrayList (Integer) - Create & Insert");
      
       System.out.println(" Enter 'n' value :");
       n = Integer.parseInt(scan.nextLine());
      
       System.out.println("Enter the data :");
      
       for(int i=0; i<n; i++) {
          
           list.add(scan.nextInt());
           list1.add(scan.nextInt());
       }
      
   }
  
   void function()
   {   list3 = new ArrayList<Integer>();
   n=list1.size();
  
  
       for(int i=0;i<n;i++)
       {
      int item = list1.get(i);
      list3.add(item);
      int item1=list3.get(i);
    list3.add(item1);
     
  
       }
      
           for(int i=0;i<=n/2;i++)
           {
           list4 = new ArrayList<Integer>();

           int item1 =list1.get(i);
           list4.add(item1);
  
   }
  
   for(int i=0;i<=n/2;i++)
   {
   int item1 =list1.get(i);

   list4.add(item1);
   }
  
   }
  
  
  
  
  
  
  
  
  
  
  
  
  
   void display() {

      
       System.out.println(" The ArrayList");
      
       if(list3.isEmpty() && list4.isEmpty()) {
          
           System.out.println("ArrayList is Empty..");
       }      
       else {
          
           for(int i=0; i<list3.size(); i++) {
              
               System.out.println(list3.get(i));
           }
      }
     
      for(int i=0; i<list4.size(); i++) {
              
               System.out.println(list4.get(i));
           }
     
   }
}

class MainClass {
  
   public static void main(String args[]) {
      
       ArrayList_Ex1 obj = new ArrayList_Ex1();
      
       obj.getVal();
       obj.display();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote