Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Mixed arrays and lists

โญ beginnerโณ 12 min read๐Ÿ“ Lesson 75 of 256

Conversions, boxing, remove-by-value vs remove-by-index, mutation discipline.

Mixed collections and arrays โ€” the exam loves asking for both in one method:

// array in, list out
public static ArrayList<Integer> positives(int[] a) {
    ArrayList<Integer> out = new ArrayList<Integer>();
    for (int x : a) {
        if (x > 0) {
            out.add(x);
        }
    }
    return out;
}

Four conversion facts that decide correctness:

  • int[] and ArrayList<Integer> are different types; neither auto-converts. Box manually (out.add(x) boxes the int).
  • list.remove(Integer.valueOf(5)) removes the VALUE 5; list.remove(5) removes INDEX 5. The exam tests this distinction.
  • Removing while iterating forward skips the element after the removed one โ€” iterate backwards or use an index-corrected loop.
  • a.length vs list.size() โ€” one is a field, one is a method.

When a spec says "return a new list", never mutate the input.

โšก Now practice

Ready to Code
Integrated repsPipelines, registers, conversions, skipping removals, aggregation state.
4 challenges ยท ยท ~55 min