A collection that grows, and files without manual buffers
CS205ES built a linked list by hand to get a resizable collection. java.util.ArrayList gives you one already built. java.io gives you file reading without ever managing a buffer yourself.
After this lesson
You should be able to
- Use an ArrayList to store and access a growing collection of values.
- Read a text file using java.io without manual buffer management.
- Explain why ArrayList can hold any size of data without the programmer resizing anything.
ArrayList: the resizable array CS105ES never had
A C array from CS105ES had one fixed size, decided at declaration and never changing — growing it meant manually allocating a bigger block and copying every element over, exactly the kind of low-level bookkeeping CS205ES's linked list was built to avoid. java.util.ArrayList does that resizing internally: add() always works, regardless of how many elements are already there, and you never see the resize happen.
This is not magic — internally, an ArrayList is doing exactly the allocate-bigger-copy-over work CS205ES made you do by hand, just hidden behind a clean interface, the same abstraction instinct from earlier in this course applied to a data structure instead of a class hierarchy.
import java.util.ArrayList;
ArrayList<Integer> marks = new ArrayList<>();
marks.add(78);
marks.add(81);
marks.add(90);
System.out.println(marks.size()); // 3, no manual tracking needed
System.out.println(marks.get(1)); // 81java.io: files, without the buffer you managed in CS105ES
File handling in CS105ES meant opening a FILE pointer, checking it against NULL, and reading with fgets into a fixed-size character buffer you had to size correctly yourself. Java's BufferedReader wraps a file and hands you one line at a time as a String, already the right length, with the buffering handled internally and invisibly.
The pattern of opening, reading until nothing is left, and closing is identical in spirit to CS105ES's file loop — only the mechanics changed, from manual buffer sizing to an object that manages it for you.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
try (BufferedReader reader = new BufferedReader(new FileReader("marks.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Could not read file: " + e.getMessage());
}Try it yourself
Use an ArrayList<String> to collect five names entered one at a time. After collecting them, loop over the list and print each name with its index.
Need a hint?
ArrayList supports the same enhanced for loop from CS303PC's own arrays lesson — but if you need the index too, a normal indexed for loop over list.size() works just as well.
Check the worked solution
An indexed loop from 0 to list.size() - 1 gives both the index and, via get(i), the value at that index — this mirrors exactly how you indexed a plain array in CS105ES, just calling get(i) instead of using square brackets.
ArrayList<String> names = new ArrayList<>();
names.add("Kavya");
names.add("Ravi");
names.add("Sita");
names.add("Arjun");
names.add("Meena");
for (int i = 0; i < names.size(); i++) {
System.out.println(i + ": " + names.get(i));
}Quick check
Why can you call add() on an ArrayList any number of times without ever manually resizing anything?
Why this lesson exists
Syllabus mapping
Exploring String class, Object class, Exploring java.util package, Exploring java.io package.
Maps to course outcome CO1.