๐ 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[]andArrayList<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.lengthvslist.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 CodeIntegrated repsPipelines, registers, conversions, skipping removals, aggregation state.
4 challenges ยท ยท ~55 min