File size: 2,490 Bytes
41932b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
Write the method

void removeNegatives(ArrayList<Integer> numnbers) 

...which removes all numbers less than zero from the list.

Tip: the for loop may not work reliably if the iterated structure changes during loop execution 
- you should use another way to iterate through the list instead.



Example method call:
public static void main(String[] parameters){
    ArrayList<Integer> numbers = new ArrayList<>();
    numbers.add(1);
    numbers.add(-2);
    numbers.add(-5);
    numbers.add(10);
    System.out.println(numbers);
    removeNegatives(numbers);
    System.out.println(numbers);
}


Program outputs:
[1, -2, -5, 10]
[1, 10]



============================


import java.util.Random;
import java.util.ArrayList;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        
        int[][] s = {{1,-2,3}, {10,-20,30,-40}, {-2,4,-6,8,-10}, {-9,-1,-8,2,7,3,-6,-4}};
        
        for (int[] pa : s) {
            ArrayList<Integer> lista = new ArrayList<>();
            for (int l : pa) lista.add(l);
            
            System.out.println("List before: ");
            System.out.println("" + lista);
            System.out.println("List after: ");
            removeNegatives(lista);
            System.out.println(lista);
            System.out.println("");
            
        }
    }
    



    // ADD
    public static void removeNegatives(ArrayList<Integer> numbers) {
        for (int i = 0; i < numbers.size(); i++) {
            if (numbers.get(i) < 0) {
                numbers.remove(i);
                // because we remove the current idx
                // adjust i by 1 (like as if we finished the previous 'i')
                // System.out.println(i);
                i--;
                // System.out.println(i);
            }
        }
    }




}








// Test1
// List before: 
// [1, -2, 3]
// List after: 
// 1
// 0
// [1, 3]

// List before: 
// [10, -20, 30, -40]
// List after: 
// 1
// 0
// 2
// 1
// [10, 30]

// List before: 
// [-2, 4, -6, 8, -10]
// List after: 
// 0
// -1
// 1
// 0
// 2
// 1
// [4, 8]

// List before: 
// [-9, -1, -8, 2, 7, 3, -6, -4]
// List after: 
// 0
// -1
// 0
// -1
// 0
// -1
// 3
// 2
// 3
// 2
// [2, 7, 3]








List before: 
[1, -2, 3]
List after: 
[1, 3]

List before: 
[10, -20, 30, -40]
List after: 
[10, 30]

List before: 
[-2, 4, -6, 8, -10]
List after: 
[4, 8]

List before: 
[-9, -1, -8, 2, 7, 3, -6, -4]
List after: 
[2, 7, 3]