← All Exercises

Java ArrayList Operations

basicjava~10min

Background

ArrayList is Java resizable array implementation, providing O(1) random access and amortized O(1) append.

Problem

Write a Java method that takes an ArrayList of integers, removes all negatives, and returns the sorted result.

Input

An ArrayList of integers with positive and negative values.

Output

A new sorted ArrayList containing only non-negative integers.

Sample tests

Sample 1

Input: [3, -1, 4, -2, 0, 5]
Expected: [0, 3, 4, 5]

Your solution

Learning objective

Learn ArrayList operations, Collections.sort, and safe filtering patterns.