Skip to main content

Arrays

Array

Think about needing to keep track of the ages of 100 people. It would be quite a hassle if we had to create separate variables for each person's age, right? That's where arrays come in handy.

Introduction

Imagine an array like a storage box that can hold many items. In our case, it's like a box that can store the ages of all 100 people in one go.

Instead of dealing with hundreds of individual variables, we use a single array to keep things organised.

Example - You have a list of ages, and the array is like a special container that neatly holds all those ages together.

Now, let's dive into the details of how arrays work!

An array like a special container that holds similar types of things, like numbers or names. This container is organized, and all the items inside are placed in a row in the computer's memory.

Key points about Java arrays:

  • It's like a neatly organised box for similar things, and each thing is called an "element."
  • Elements are stored in a continuous line in the computer's memory.
  • The array is index-based, which means each element has a number associated with it. The first element is at index 0, the second at index 1, and so on.
  • Unlike some other programming languages, in Java, you can easily find out how many elements are in the array using the "length" property.
  • Java arrays are smart – they're objects that can do cool things, and they inherit traits from the Object class. They can even be Serializable and Cloneable, which are advanced features you might explore later.
  • You can store not only regular values but also more complex things like objects in a Java array.
  • Java lets you create arrays with one dimension (like a simple list) or multiple dimensions (think of a table with rows and columns).

And here's a cool extra: Java offers something called "anonymous arrays," a feature that makes your code more flexible. This is something you won't find in other languages like C/C++.

Here's how the array index looks like:

Instead of saying "first," "second," and so on, we use numbers known as "indices" to indicate the location of each item in the array. The twist is, in many programming languages, including Java, we commence counting from 0. Thus, the initial item is at index 0, the second at index 1, and so forth. It's akin to assigning a distinct address to each element based on its position.

Advantages:

  1. Code Optimisation: Arrays help optimise code, allowing for efficient retrieval and sorting of data.
  2. Random Access: You can easily access any data by specifying its index position in the array.

Disadvantages:

  1. Size Limit: Arrays have a fixed size, meaning you can only store a predefined number of elements. Unlike some other data structures, arrays don't grow in size dynamically during runtime. To overcome this limitation, Java uses the collection framework, which automatically adjusts its size as needed.

How to declare, instantiate and initialise an array?

You can declare an array in different ways:

Syntax to Declare an Array in Java:

dataType arr[]; OR dataType[] arr; OR dataType []arr;

These three forms are all valid and equivalent. They tell Java that you want to create an array that can store elements of a specific data type.

Instantiating an Array in Java:

Once you've declared an array, you need to create it in memory. Here's how you do it:

arr = new dataType[size];
  • arr is the reference variable that will point to the newly created array.
  • dataType is the type of data the array will hold.
  • size is the number of elements the array can store.

This line of code sets up the memory space for your array, making it ready to hold the specified type of elements in the specified quantity.

Initialisation

Creating an array is like making a list of things and putting them inside curly braces {}. In Java, each thing has a number assigned to it, and we call this number the "array index."

int[] age = {24, 23, 18, 19, 20};

Here's a breakdown:

  • age is the name of our list.
  • [] means it's an array.
  • {24, 23, 18, 19, 20} are the things in our list (in this case, ages).
  • int tells us that our list can only have whole numbers (integers).

Now, to grab one of these things from our list, we use the array index. The trick is, we start counting from 0. So, the first thing is at index 0, the second at index 1, and so on.

To get something from our list, we say:

// Grab the first thing
age[0];

// Grab the second thing
age[1];

It's like saying, "Give me the thing at index 0" or "Give me the thing at index 1." That's how we use array indices to get the stuff from our list!