Spaces:
Running
Running
File size: 2,993 Bytes
c0b7c5c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | Write the method
ArrayList<Integer> combine(ArrayList<Integer> list1, ArrayList<Integer> list2)
which takes as parameters two lists of integers.
The lists are ordered in ascending order (i.e. the smallest number is first and the largest last).
The method returns a new list of integer type with all the elements of the parameter lists combined.
In the new list, the elements are also ordered in order of magnitude.
Example method call:
public static void main(String[] args){
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(3);
list1.add(5);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(4);
list2.add(6);
ArrayList<Integer> combined = combine(list1, list2);
System.out.println(combined);
}
Program outputs:
[1, 2, 3, 4, 5, 6]
======================================
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Test{
public static void main(String[] args){
final Random r = new Random();
testaa(new int[]{1,3,5,7,9}, new int[]{2,4,6,8,10});
testaa(new int[]{10,20,30,40,50,60}, new int[]{70,80,90,100,110});
testaa(new int[]{1,11,21,31,41}, new int[]{2,3,4,32,33,34,42});
}
public static void tulosta(ArrayList<String> lista) {
System.out.print("[\"");
System.out.print(String.join("\", \"", lista));
System.out.println("\"]");
}
public static void testaa(int[] l1, int[] l2) {
ArrayList<Integer> lista1 = new ArrayList<>();
for (int a : l1) {
lista1.add(a);
}
ArrayList<Integer> lista2 = new ArrayList<>();
for (int a : l2) {
lista2.add(a);
}
System.out.println("Testing with lists ");
System.out.println("" + lista1);
System.out.println("" + lista2);
System.out.println("Combined: ");
System.out.println(combine(lista1, lista2));
System.out.println("");
}
// https://stackoverflow.com/a/189569/5324726
// combine used by testaa
public static ArrayList<Integer> combine(ArrayList<Integer> list1, ArrayList<Integer> list2){
ArrayList<Integer> combined = new ArrayList<>();
// for (int a : list1) {
// combined.add(a);
// }
// for (int a : list2) {
// combined.add(a);
// }
combined.addAll(list1);
combined.addAll(list2);
Collections.sort(combined);
return combined;
}
}
Testing with lists
[1, 3, 5, 7, 9]
[2, 4, 6, 8, 10]
Combined:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Testing with lists
[10, 20, 30, 40, 50, 60]
[70, 80, 90, 100, 110]
Combined:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
Testing with lists
[1, 11, 21, 31, 41]
[2, 3, 4, 32, 33, 34, 42]
Combined:
[1, 2, 3, 4, 11, 21, 31, 32, 33, 34, 41, 42]
|