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