From b4d915e6214c459b62716912a3471e15ffdf7b35 Mon Sep 17 00:00:00 2001 From: Markus Trietz <mtrietz@yahoo.de> Date: Tue, 1 Oct 2024 08:08:00 +0000 Subject: [PATCH] Upload New File --- FixMe.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 FixMe.java diff --git a/FixMe.java b/FixMe.java new file mode 100644 index 0000000..79a2301 --- /dev/null +++ b/FixMe.java @@ -0,0 +1,35 @@ +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++; + } + } + } +} -- GitLab