Day 3: Arrays – Java’s Special Way of Annoying You

·

4 min read

Alright, today I tackled arrays in Java. Sounds simple, right? Just a collection of items, nice and neat. But oh no, Java had to make it just a little bit more complicated for us.


What Even is an Array?

An array is basically a list—a way to store multiple values in a single variable. Instead of writing:

int num1 = 10;
int num2 = 20;
int num3 = 30;

(Java would love if you suffered like this)

You just do:

int[] numbers = {10, 20, 30};

Boom. One line. Clean. Efficient. No unnecessary pain.

But don’t get too excited—arrays in Java come with rules. And Java loves its rules.


Arrays are FIXED in Size (Because Why Not?)

Java says, “You must declare the array’s size beforehand, and you cannot change it later.”

int[] numbers = new int[5]; // Array of size 5, nothing more, nothing less.

So if you suddenly need to add a 6th number? Too bad. Java won’t let you. You’ll have to create a whole new array, copy everything over, and then add your new value. Because Java believes in making us work harder.


How to Access Elements in an Array?

Java has this wonderful thing called indexing, where the first element starts at 0 (not 1, because that would be too logical).

Example:

int[] numbers = {10, 20, 30, 40, 50};

System.out.println(numbers[0]); // Prints 10 (first element)
System.out.println(numbers[2]); // Prints 30 (third element)

Try accessing something outside the array’s range, and Java will explode with an ArrayIndexOutOfBoundsException because Java loves reminding us of our mistakes.


Changing Values in an Array

Want to replace an element? Easy. Just assign a new value to an index.

numbers[1] = 100; // Changes the second element from 20 to 100

Now, the array is {10, 100, 30, 40, 50}. Simple, right?


Looping Through an Array (Because We Are Not Cavemen)

Imagine manually printing each element… yeah, no. That’s where loops save our sanity.

The Classic For Loop

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

This prints all the elements without making you suffer.

The For-Each Loop (For When You’re Feeling Lazy)

for (int num : numbers) {
    System.out.println(num);
}

This is like telling Java:
"Hey, just print each number. You figure out the details."
Java does it. No questions asked.


Types of Arrays (Because One Wasn’t Enough)

String Array (For Names and Words)

String[] names = {"Alice", "Bob", "Charlie"};

Character Array (For Individual Letters)

char[] vowels = {'a', 'e', 'i', 'o', 'u'};

Boolean Array (For True/False Values)

boolean[] flags = {true, false, true};

Double Array (For Decimal Numbers)

double[] prices = {10.99, 20.50, 30.75};

Because Java believes in variety.


Multidimensional Arrays – Arrays Inside Arrays (Why, Java? Just Why?)

Because one array wasn’t annoying enough, Java lets you have arrays inside arrays.

int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
System.out.println(matrix[1][2]); // Prints 6 (row 1, column 2)

Why would you need this? I don’t know. But it exists.


Common Mistakes in Arrays (Because Learning Isn’t Fun Without Errors)

1️⃣ Forgetting Arrays Start at 0

int[] arr = {10, 20, 30};
System.out.println(arr[3]); // ERROR! Java yells at you.

Solution: Always check .length before accessing indexes.

2️⃣ Trying to Change Array Size (Haha, Nope)

int[] numbers = new int[5];
numbers[5] = 100; // ERROR! Arrays are fixed size.

Solution: Use an ArrayList instead (but that’s for another day).

3️⃣ Thinking = Copies Arrays (Spoiler: It Doesn’t)

int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]); // Prints 99 (Wait, what?!)

Arrays in Java don’t make copies—they share memory. Change one, and the other changes too.


Why Use Arrays?

✅ Store multiple values without multiple variables
✅ Organize data neatly
✅ Loop through elements easily
✅ Use them for sorting, searching, and processing data

But also...
❌ Can’t change size
❌ Java yells at you for going out of bounds
❌ Multidimensional arrays exist for some reason


Final Thoughts

Arrays in Java are powerful, but also kind of annoying. The key takeaway? Think of them as numbered lockers—you can store things, retrieve them by their number, but you can’t magically add more lockers.

And that’s Day 3 of my DSA journey! 🎉
Now time to solve array problems (and question my life choices).