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
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
void removeNegatives(ArrayList<Integer> numnbers)
|
| 4 |
+
|
| 5 |
+
...which removes all numbers less than zero from the list.
|
| 6 |
+
|
| 7 |
+
Tip: the for loop may not work reliably if the iterated structure changes during loop execution
|
| 8 |
+
- you should use another way to iterate through the list instead.
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
Example method call:
|
| 13 |
+
public static void main(String[] parameters){
|
| 14 |
+
ArrayList<Integer> numbers = new ArrayList<>();
|
| 15 |
+
numbers.add(1);
|
| 16 |
+
numbers.add(-2);
|
| 17 |
+
numbers.add(-5);
|
| 18 |
+
numbers.add(10);
|
| 19 |
+
System.out.println(numbers);
|
| 20 |
+
removeNegatives(numbers);
|
| 21 |
+
System.out.println(numbers);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
Program outputs:
|
| 26 |
+
[1, -2, -5, 10]
|
| 27 |
+
[1, 10]
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
============================
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
import java.util.Random;
|
| 35 |
+
import java.util.ArrayList;
|
| 36 |
+
|
| 37 |
+
public class Test{
|
| 38 |
+
public static void main(String[] args){
|
| 39 |
+
final Random r = new Random();
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
int[][] s = {{1,-2,3}, {10,-20,30,-40}, {-2,4,-6,8,-10}, {-9,-1,-8,2,7,3,-6,-4}};
|
| 43 |
+
|
| 44 |
+
for (int[] pa : s) {
|
| 45 |
+
ArrayList<Integer> lista = new ArrayList<>();
|
| 46 |
+
for (int l : pa) lista.add(l);
|
| 47 |
+
|
| 48 |
+
System.out.println("List before: ");
|
| 49 |
+
System.out.println("" + lista);
|
| 50 |
+
System.out.println("List after: ");
|
| 51 |
+
removeNegatives(lista);
|
| 52 |
+
System.out.println(lista);
|
| 53 |
+
System.out.println("");
|
| 54 |
+
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
// ADD
|
| 62 |
+
public static void removeNegatives(ArrayList<Integer> numbers) {
|
| 63 |
+
for (int i = 0; i < numbers.size(); i++) {
|
| 64 |
+
if (numbers.get(i) < 0) {
|
| 65 |
+
numbers.remove(i);
|
| 66 |
+
// because we remove the current idx
|
| 67 |
+
// adjust i by 1 (like as if we finished the previous 'i')
|
| 68 |
+
// System.out.println(i);
|
| 69 |
+
i--;
|
| 70 |
+
// System.out.println(i);
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
// Test1
|
| 88 |
+
// List before:
|
| 89 |
+
// [1, -2, 3]
|
| 90 |
+
// List after:
|
| 91 |
+
// 1
|
| 92 |
+
// 0
|
| 93 |
+
// [1, 3]
|
| 94 |
+
|
| 95 |
+
// List before:
|
| 96 |
+
// [10, -20, 30, -40]
|
| 97 |
+
// List after:
|
| 98 |
+
// 1
|
| 99 |
+
// 0
|
| 100 |
+
// 2
|
| 101 |
+
// 1
|
| 102 |
+
// [10, 30]
|
| 103 |
+
|
| 104 |
+
// List before:
|
| 105 |
+
// [-2, 4, -6, 8, -10]
|
| 106 |
+
// List after:
|
| 107 |
+
// 0
|
| 108 |
+
// -1
|
| 109 |
+
// 1
|
| 110 |
+
// 0
|
| 111 |
+
// 2
|
| 112 |
+
// 1
|
| 113 |
+
// [4, 8]
|
| 114 |
+
|
| 115 |
+
// List before:
|
| 116 |
+
// [-9, -1, -8, 2, 7, 3, -6, -4]
|
| 117 |
+
// List after:
|
| 118 |
+
// 0
|
| 119 |
+
// -1
|
| 120 |
+
// 0
|
| 121 |
+
// -1
|
| 122 |
+
// 0
|
| 123 |
+
// -1
|
| 124 |
+
// 3
|
| 125 |
+
// 2
|
| 126 |
+
// 3
|
| 127 |
+
// 2
|
| 128 |
+
// [2, 7, 3]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
List before:
|
| 138 |
+
[1, -2, 3]
|
| 139 |
+
List after:
|
| 140 |
+
[1, 3]
|
| 141 |
+
|
| 142 |
+
List before:
|
| 143 |
+
[10, -20, 30, -40]
|
| 144 |
+
List after:
|
| 145 |
+
[10, 30]
|
| 146 |
+
|
| 147 |
+
List before:
|
| 148 |
+
[-2, 4, -6, 8, -10]
|
| 149 |
+
List after:
|
| 150 |
+
[4, 8]
|
| 151 |
+
|
| 152 |
+
List before:
|
| 153 |
+
[-9, -1, -8, 2, 7, 3, -6, -4]
|
| 154 |
+
List after:
|
| 155 |
+
[2, 7, 3]
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|