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
Please use the template below and use the number from above: Dr.java
/**
* Auto Generated Java Class.
*/
import java.util.ArrayList;
public class arrayListLab
{
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("merge returns "+merge(a,b));
}
public static ArrayList merge(ArrayList x, ArrayList y)
{
// create a merging algorithm and code.
ArrayList temp = new ArrayList(x.size()+y.size());
for(Integer i :x)
{
temp.add(i);
}
for(Integer i : y)
{
temp.add(i);
}
return(temp);
}
}
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...
//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.
A java exercise please!
Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...
Questions Write a method that returns the union of two array lists of integers using the following header: public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) Write a test program that: 1. prompts the user to enter two lists, each with five integers, 2. displays their union numbers separated by exactly one space., and 3. finds the maximum number and the minimum number in the combined list. Here is a sample run of the program. Enter five integers for list1: 5...
Given 5. import java.util. 6. public class Sortof 7. public static void main(Stringl args) [ 8. ArrayList Integer> anew ArrayList Integer 0 9. a.add(1); а.add(5); a.add(3); 11. Collections.sort(a) 12. a.add(2); 13. Collections.reverse(a) 14. System.out.printin (a) 15 16 What is the result? B. 12, 1, 3, 5] C. 12, 5, 3, 1] E. [1, 3, 5, 2 F. Compilation fails G. An exception is thrown at runtime
Given 5. import java.util. 6. public class Sortof 7. public static void main(Stringl args)...
Problem 3 Implement a mergeLists method in LinkedListTest class that merges 2 given lists and returns an output list. While merging make sure: 1. Output list is sorted 2. Remove any duplicate Example: listl: 2 479 list2: 4 5 167 merge (listl, list2) Output: 1 2 45679
Code in java
Using the template below:
public class Lab03 {
public static void main(String[] args) {
ArrayList<Integer> list1 = new
ArrayList<Integer>();
Collections.addAll(list1, 1, 3, 5, 5 );
ArrayList<Integer> list2 = new
ArrayList<Integer>();
Collections.addAll(list2, 3, 7, 3, 2, 4 );
ArrayList<Integer> result1= uniqueUnion(list1,list2);
System.out.println(result1);
Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2,
2};
ArrayList<Integer> arrayList = new
ArrayList<Integer>(Arrays.asList(array));
System.out.printf("The average is: %.2f%n",
averagePositive(arrayList));
} // end main
public static ArrayList<Integer>
uniqueUnion(ArrayList<Integer> list1,
ArrayList<Integer> list2) {...
*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 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
import java.util.Scanner; public class Chpt7_Project { public static void bubbleSort(int[] list) { int temp; for (int i = list.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } ...