So far, we only know how to store data in variables, which store a
single piece of data and gives the data a name that we can later
reference. But what happens if we want to store large amounts of
data (e.g., user accounts, leaderboards, text messages, etc.)?
Assigning variables for every piece of data would be impractical and
impossible, especially for massive tech companies like Google, where
thousands of data points are generated every second. In these cases,
we need to use a data structure, a programming structure
designed to hold data. The difference between data structures and
variables is that data structures can contain many pieces of data at
once and, depending on how the data structure is formatted, enable
the programmer to manipulate the data effectively.
An array is the most basic and widely used data structure
designed to hold a fixed number of data points (called the array
size) of the same type in contiguous memory locations. The fixed
number and data type need to be provided when the array is created,
but the data itself can be manipulated and modified later, which is
what makes an array powerful.
Note: Python does not support arrays and instead uses a data
structure called a List, which is similar to a Vector data structure
in C++ or an ArrayList data structure in Java. All three are helpful
for a variety of reasons that are outside the scope of this event.
We encourage everyone to Google information about them to see what
benefits they provide!
This is how an array named
arr
of size 10 is created with data type
int
:
Code:
int[] arr = new int[10];
Code:
int arr[10] = {};
Note: Initializing an array using this method sets all of the
elements to the default value of 0.
An array can be treated as a sequence of variables (called
elements) of the provided data type, but instead of accessing
each variable by name, they are accessed using the array’s name with
an index. Java and C++ are zero-indexed, meaning
arrays start with index 0.
With this scheme, the first element in an array is accessed with the
syntax
arr[0]
, the 2nd element is accessed with
arr[1]
, the 3rd element is accessed with
arr[2]
, and so on.
Common Mistake: Zero-indexing is challenging to grasp initially
because we are raised on numbers starting with 1. Most, but not all,
languages are 0-indexed. JavaScript and Python are also 0-indexed,
but MATLAB is 1-indexed.
Once you have accessed an element in your array, you can manipulate
it as if it was a normal variable of the array’s data type. For
example:
We can modify it:
Code:
int[] arr = new int[10];
arr[0] = 56;
System.out.println(arr[0]);
Output:
> 56
Code:
int arr[10] = {};
arr[0] = 56;
cout << arr[0];
Output:
> 56
We can add them (and use all of the other arithmetic operators):
One of the most important uses of arrays is in conjunction with
loops. Recall that for-loops enable you to run the same block of
code multiple times with an iterator variable. We can use that to
perform an operation on every element of an array. For example, we
could double the value of every element:
A more helpful operation that a programmer might see in the real
world could be connecting two String arrays. In this example, we
combine two String arrays consisting of first and last names to get
an array of full names.
Much like nested for-loops and if-statements, we can nest arrays
inside one another by making an array where the data type is another
array. This is called a 2D array.
Let’s look at this example, where we initialize an array of size 3,
where its data type is another array, but this time with data type
int
and size 4.
Array named
arr
(We will refer to this array as the “outer” array)
Size: 3
Data type: Array (We will refer to any of these arrays as
“inner” arrays)
Size: 4
Data type:
int
Code:
int[][] arr = new int[3][4];
Code:
int arr[3][4] = {};
In our original 1D array, we used an index to access certain
elements inside the array. This also works for our 2D array. Using a
single index allows us to access an element inside the array.
However, because in 2D arrays the data type of the outer array is
another array, the element we access using a single index is just an
inner array. So, we need to go one step deeper using a second index
to access an element of the inner array. This code accesses the
element located at index 3 of the inner array located at index 0 of
the outer array
Code:
arr[0][3];
Code:
arr[0][3];
Once you have used two indices, every element of an inner array can
be manipulated like a variable just like with our normal arrays:
Code:
int[][] arr = new int[10][12];
arr[5][2] = 56;
System.out.println(arr[5][2]);
Output:
> 56
Code:
int arr[10][12] = {};
arr[5][2] = 56;
cout << arr[5][2];
Output:
> 56
2D arrays are visually represented using a 2D grid where the rows
are each of the inner arrays. This, for example, is a depiction of a
3x4 2D array. There are three arrays of size 4, one in each row.
They are also primarily used to store data that were initially
designed with two dimensions in mind. For example, a checkerboard
can be represented with an 8x8 array.
Note: If you are using multi-dimensional arrays, be careful with
declaring too many dimensions. Every additional dimension increases
the memory required to store the array exponentially.
You can play with all the code we've used in this article on Replit: