C
JV

C to Java

10 lessons

Progress0%
1Introduction to Java2Data Types3Strings4Arrays and Collections5Object-Oriented Programming6Exception Handling7Collections and Generics8Modern Java Features9Interfaces and Polymorphism10Threads and Concurrency
All Mirror Courses
C
JV
Arrays and Collections
MirrorLesson 4 of 10
Lesson 4

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.

Mirror Card
C
From C:

In C, you're familiar with array handling and collections.

JV
In Java:

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:

JV
Java 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); // 1

Comparing to C

Here's how you might have written similar code in C:

C
C (What you know)
// 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);
Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

.length property stores array size

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Bounds checking prevents buffer overflows

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

No pointer arithmetic on arrays

Mirror Card
C
From C:

You may be used to different syntax or behavior.

JV
In Java:

Collections (ArrayList, etc.) for dynamic sizing

Step-by-Step Breakdown

1. Array Declaration

Java arrays know their own length and check bounds.

JV
Java
// 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); // 5

2. ArrayList

ArrayList is a resizable array. It's the closest thing to realloc.

C
C
// C - manual resizing
int* arr = malloc(10 * sizeof(int));
arr = realloc(arr, 20 * sizeof(int));
JV
Java
// 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 size
Rule of Thumb
Use arrays when size is fixed and known. Use ArrayList when you need dynamic sizing.

3. Iterating Arrays

Java provides enhanced for loops and forEach.

C
C
for (int i = 0; i < size; i++) {
    printf("%d\n", arr[i]);
}
JV
Java
// 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
Common Pitfall
Don't assume Java works exactly like C. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Arrays have .length property
  • Bounds are checked at runtime
  • Use ArrayList for dynamic sizing
  • Enhanced for-each simplifies iteration
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C code in Java to practice these concepts.
PreviousNext