Spaces:
Running
Running
ArrayList<Integer> lista = new ArrayList<>(); for (int l : pa) lista.add(l);
Browse files
Week 3: Objects, files and exceptions/3b. Add sum to list - RECREATE LIST
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
void addSum(ArrayList<Integer> numbers)
|
| 4 |
+
|
| 5 |
+
...which calculates the sum of the numbers in the list and adds it as the last element of the list.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
Example method call:
|
| 9 |
+
public static void main(String[] parameters){
|
| 10 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 11 |
+
for (int i=1; i<5; i++) {
|
| 12 |
+
numbers.add(i);
|
| 13 |
+
}
|
| 14 |
+
System.out.println(numbers);
|
| 15 |
+
addSum(numbers);
|
| 16 |
+
System.out.println(numbers);
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
Program outputs:
|
| 20 |
+
[1, 2, 3, 4]
|
| 21 |
+
[1, 2, 3, 4, 10]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
===============================================
|
| 25 |
+
|
| 26 |
+
import java.util.Random;
|
| 27 |
+
import java.util.ArrayList;
|
| 28 |
+
|
| 29 |
+
public class Test{
|
| 30 |
+
public static void main(String[] args){
|
| 31 |
+
final Random r = new Random();
|
| 32 |
+
|
| 33 |
+
int[][] s = {{1,2,3}, {10,20,30,40}, {2,4,6,8,10}, {9,1,8,2,7,3,6,4}};
|
| 34 |
+
|
| 35 |
+
for (int[] pa : s) {
|
| 36 |
+
|
| 37 |
+
// if we do not recreate a new list
|
| 38 |
+
// then go through each element to add then to our new list
|
| 39 |
+
// we get the references
|
| 40 |
+
// [I@29ba4338
|
| 41 |
+
// [I@57175e74
|
| 42 |
+
// [I@7bb58ca3
|
| 43 |
+
// [I@c540f5a
|
| 44 |
+
ArrayList<Integer> lista = new ArrayList<>();
|
| 45 |
+
for (int l : pa) lista.add(l);
|
| 46 |
+
// vs recreating a new list for each sub-list
|
| 47 |
+
// [1, 2, 3]
|
| 48 |
+
// [10, 20, 30, 40]
|
| 49 |
+
// [2, 4, 6, 8, 10]
|
| 50 |
+
// [9, 1, 8, 2, 7, 3, 6, 4]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
System.out.println("List before: ");
|
| 54 |
+
System.out.println("" + lista);
|
| 55 |
+
System.out.println("List after: ");
|
| 56 |
+
addSum(lista);
|
| 57 |
+
System.out.println(lista);
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
//ADD
|
| 63 |
+
// dont return anything
|
| 64 |
+
// cuz we update the list inplace
|
| 65 |
+
public static void addSum(ArrayList<Integer> numbers) {
|
| 66 |
+
int sum = 0;
|
| 67 |
+
for (int i : numbers) {
|
| 68 |
+
sum += i;
|
| 69 |
+
}
|
| 70 |
+
numbers.add(sum);
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
List before:
|
| 87 |
+
[1, 2, 3]
|
| 88 |
+
List after:
|
| 89 |
+
[1, 2, 3, 6]
|
| 90 |
+
List before:
|
| 91 |
+
[10, 20, 30, 40]
|
| 92 |
+
List after:
|
| 93 |
+
[10, 20, 30, 40, 100]
|
| 94 |
+
List before:
|
| 95 |
+
[2, 4, 6, 8, 10]
|
| 96 |
+
List after:
|
| 97 |
+
[2, 4, 6, 8, 10, 30]
|
| 98 |
+
List before:
|
| 99 |
+
[9, 1, 8, 2, 7, 3, 6, 4]
|
| 100 |
+
List after:
|
| 101 |
+
[9, 1, 8, 2, 7, 3, 6, 4, 40]
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|