Question: write a java method [public static ArrayList merge (ArrayList a, ArrayList b)] that merges two array lists, alternating elements from both array lists. if one

write a java method [public static ArrayList merge (ArrayList a, ArrayList b)] that merges two array lists, alternating elements from both array lists. if one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11, then merge returns the array lists 1 9 4 7 9 4 16 9 11 PLEASE USE THE BASE CODE BELOW PROVIDED

import java.util.ArrayList;

public class myARRAYLIST { public static void main(String[] args) { ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); // Initialize array list a to some values for (int i = 0; i < 4; i++) { a.add((i + 1) * (i + 1)); } // Initialize array list b to some values b.add(9); b.add(7); b.add(4); b.add(9); b.add(11); b.add(21); System.out.println("ArrayList b is "+b); System.out.println("ArrayList a is " +a); System.out.println("merged is" +merge (a,b));

} public static ArrayList merge (ArrayListx, ArrayListy) { ArrayList temp = new ArrayList (x.size() + y.size () ); return temp; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!