Lesson 16 of 34

ArrayList: A List That Grows

9 min 30 XP

An array has a fixed size. When you don't know how many items you'll have, use an ArrayList, which grows by itself. Write Integer instead of int and Double instead of double inside the < >.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Array: arr.length (no brackets). String: s.length(). ArrayList: list.size(). Don't mix these three up in an interview!

Challenge

+30 XP

Read n and n numbers. Put only the even numbers into an ArrayList and print it. Input: 6 1 4 7 10 12 3 Output: [4, 10, 12]

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.