Question

Implement the following methods (based on ArrayLists): (JAVA) a) merge method that takes two ArrayLists as...

Implement the following methods (based on ArrayLists): (JAVA)
a) merge method that takes two ArrayLists as input and returns the merged ArrayList

containing the elements from both the input lists.
b) enumerate method that takes an ArrayList as input and prints the enumerated

contents of the input list.
c) reverse method that takes an ArrayList as input and returns an ArrayList with the

contents of input list in reverse order.
Test your method implementations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

public class Main {

public static ArrayList<Integer> merge(ArrayList<Integer> a1, ArrayList<Integer> a2) {

ArrayList<Integer> res = new ArrayList<>();

res.addAll(a1);

res.addAll(a2);

return res;

}

public static void enumerate(ArrayList<Integer> a) {

for(int elem : a) {

System.out.print(elem + " ");

}

System.out.println("\n");

}

public static ArrayList<Integer> reverse(ArrayList<Integer> a) {

Collections.reverse(a);

return a;

}

public static void main(String[] args) {

ArrayList<Integer> a1 = new ArrayList<>();

ArrayList<Integer> a2 = new ArrayList<>();

a1.addAll(Arrays.asList(1, 2, 3, 4, 5));

a2.addAll(Arrays.asList(10, 20, 30, 40, 50));

enumerate(a1);

enumerate(a2);

ArrayList<Integer> merged = merge(a1, a2);

enumerate(merged);

reverse(merged);

enumerate(merged);

}

}

Add a comment
Know the answer?
Add Answer to:
Implement the following methods (based on ArrayLists): (JAVA) a) merge method that takes two ArrayLists as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a 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 list 1 9 4 7 9 4 16 9 11...

  • Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement...

    Lab #4 – Recursive Methods for Generic ArrayList ----- JAVA The problem Use recursion to implement some list functionalities on an ArrrayList of generic type using type parameters in method definition Our goal for this lab is to continue using the divide and conquer approach of splitting a list into its head and tail and keep recursing on the tail (which happens to be smaller). However, instead of trying the approach on a string (a list of characters) we would...

  • java code Write a method called reverse that takes an array of integers as an input...

    java code Write a method called reverse that takes an array of integers as an input and returns the same values in reverse order as the output. Test your method in the main.

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked...

    *********************Java recursion********************** Plz help me write a method in java that traverse Through a integer linked list recursively . The method returns a string with every element in the list and a space in between each element in reverse order. THE METHOD DOES NOT REVERSE THE LINKED LIST. IT JUST RETURNS A STRING CONTAINING THE ELEMENTS OF THE LIST IN REVERSE ORDER. ASSUME ALL THE ELEMENTS IN THE LINKED LIST ARE int... method signature is something like : public String...

  • Java: please ignore my answer, do question 3. Question 3. 15 pts. The equalsO method of...

    Java: please ignore my answer, do question 3. Question 3. 15 pts. The equalsO method of the ArrayList<E> class takes another list as a parameter and returns true if the two lists contain the same elements in the same order, and false otherwise middle section of the code below so that the output is truetrue . Remember that add (int index, E element), and set (int index, E element). you can put elements into an ArrayList with the methods add(E...

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT