Skip to content
Snippets Groups Projects
Commit ecdb0d01 authored by Kreshnik Petrela's avatar Kreshnik Petrela
Browse files

Add new file

parent f127db50
No related branches found
No related tags found
No related merge requests found
import java.util.ArrayList;
import java.util.List;
public class FixMe {
public static void main(String[] args) {
// Test the method with different inputs
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(-1);
list.add(3);
list.add(-4);
list.add(-5);
list.add(6);
removeNegatives(list);
System.out.println(list);
}
/**
* The method removes all elements smaller than zero from the list
* @param list the list from which elements are removed
*/
public static void removeNegatives(ArrayList<Integer> list) {
int index = 0;
while (index < list.size()) {
int element = list.get(index);
if (element < 0) {
list.remove(index);
} else {
index++;
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment