Sponsored by Replit
Arrays
How to store and manipulate a lot of data at once
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.
arrays-img-1.png
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):
Code:
int[] arr = new int[10];
arr[0] = 3;
arr[1] = 2;
System.out.println(arr[0] + arr[1]);
Output:
> 5
Code:
int arr[10] = {};
arr[0] = 3;
arr[1] = 2;
cout << arr[0] + arr[1];
Output:
> 5
  • We can use it in its own modification equation:
Code:
int[] arr = new int[10];
arr[0] = 5;
arr[0] = arr[0] + 2;
System.out.println(arr[0]);
Output:
> 7
Code:
int arr[10] = {};
arr[0] = 5;
arr[0] = arr[0] + 2;
cout << arr[0];
Output:
> 7
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:
Code:
int[] arr = new int[10];
arr[0] = 4;
arr[1] = 2;
arr[2] = 0;
arr[3] = 5;
arr[4] = 7;
arr[5] = 11;
arr[6] = 6;
arr[7] = 3;
arr[8] = 1;
arr[9] = 9;
for(int i = 0; i < 10 ;i++) {
    arr[i] = arr[i] * 2;
    System.out.println(arr[i]);
}
Output:
> 8
> 4
> 0
> 10
> 14
> 22
> 12
> 6
> 2
> 18
Code:
int arr[10] = {};
arr[0] = 4;
arr[1] = 2;
arr[2] = 0;
arr[3] = 5;
arr[4] = 7;
arr[5] = 11;
arr[6] = 6;
arr[7] = 3;
arr[8] = 1;
arr[9] = 9;
for(int i = 0; i < 10 ;i++) {
    arr[i] = arr[i] * 2;
    cout << arr[i] << "\n";
}
Output:
> 8
> 4
> 0
> 10
> 14
> 22
> 12
> 6
> 2
> 18
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.
Code:
String[] firstNames = {"Susan", "John", "Tracy"};
String[] lastNames = {"Milton", "Smith", "Chang"};
String[] fullNames = new String[3];

for(int i = 0; i < 3; i++) {
    fullNames[i] = firstNames[i] + " " + lastNames[i];
    System.out.println(fullNames[i]);
}
Output:
> Susan Milton
> John Smith
> Tracy Chang
Code:
string firstNames[3] = {"Susan", "John", "Tracy"};
string lastNames[3] = {"Milton", "Smith", "Chang"};
string fullNames[3];

for(int i = 0; i < 3; i++) {
    fullNames[i] = firstNames[i] + " " + lastNames[i];
    cout << fullNames[i] << "\n";
}
Output:
> Susan Milton
> John Smith
> Tracy Chang
2D Arrays
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.
arrays-img-2.jpeg
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.
arrays-img-3.png
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:
Join our discord server for updates, announcements, future event planning, to ask questions, or just for fun!
Copyright ©2021 Howard County Hour of Code