Arrays and Collections
Array handling and collections
Introduction
In this lesson, you'll learn about arrays and collections in Java. Coming from C, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C, you're familiar with array handling and collections.
Java has its own approach to array handling and collections, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
// Java arrays
int[] arr = {1, 2, 3, 4, 5};
int[] dynamic = new int[10];
// Size is part of array
int size = arr.length;
// Access
arr[0] = 10;
// No pointer arithmetic
// Bounds checking!
// arr[100] = 1; // ArrayIndexOutOfBoundsException
// Collections for dynamic sizing
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.get(0); // 1Comparing to C
Here's how you might have written similar code in C:
// C arrays
int arr[5] = {1, 2, 3, 4, 5};
int* dynamic = malloc(10 * sizeof(int));
// Size not stored with array
int size = 5; // must track separately
// Access
arr[0] = 10;
*(arr + 1) = 20; // pointer arithmetic
// No bounds checking!
arr[100] = 1; // undefined behavior
free(dynamic);You may be used to different syntax or behavior.
.length property stores array size
You may be used to different syntax or behavior.
Bounds checking prevents buffer overflows
You may be used to different syntax or behavior.
No pointer arithmetic on arrays
You may be used to different syntax or behavior.
Collections (ArrayList, etc.) for dynamic sizing
Step-by-Step Breakdown
1. Array Declaration
Java arrays know their own length and check bounds.
// Declaration styles
int[] arr1 = new int[5]; // initialized to 0s
int[] arr2 = {1, 2, 3, 4, 5}; // with values
// Multi-dimensional
int[][] matrix = new int[3][3];
int[][] jagged = {{1}, {2, 3}, {4, 5, 6}};
// Length is a property
System.out.println(arr2.length); // 52. ArrayList
ArrayList is a resizable array. It's the closest thing to realloc.
// C - manual resizing
int* arr = malloc(10 * sizeof(int));
arr = realloc(arr, 20 * sizeof(int));// Java - automatic resizing
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.get(0); // 1
list.set(0, 10); // change first element
list.remove(0); // remove first element
list.size(); // current size3. Iterating Arrays
Java provides enhanced for loops and forEach.
for (int i = 0; i < size; i++) {
printf("%d\n", arr[i]);
}// Traditional
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
// Enhanced for-each
for (int value : arr) {
System.out.println(value);
}
// With ArrayList
list.forEach(item -> System.out.println(item));Common Mistakes
When coming from C, developers often make these mistakes:
- .length property stores array size
- Bounds checking prevents buffer overflows
- No pointer arithmetic on arrays
Key Takeaways
- Arrays have .length property
- Bounds are checked at runtime
- Use ArrayList for dynamic sizing
- Enhanced for-each simplifies iteration