<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
  <iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="allowfullscreen" loading="eager" referrerpolicy="strict-origin-when-cross-origin" src="" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="YouTube video"

Post Office Boxes Post Office Boxes^[File:USPS Post office boxes 1.jpg. (2017, May 17). Wikimedia Commons, the free media repository. Retrieved 18:17, November 5, 2018 from]

Arrays allow us to store multiple values in the same variable, using an index to determine which value we wish to store or retrieve from the array. We can think of arrays like a set of post office boxes. Each one has the same physical address, the post office, but within the post office we can find an individual box based on its own box number.

Some programming languages, such as Java, use arrays that are statically sized when they are first created, and those arrays cannot be resized later. In addition, many languages that require variables to be declared with a type only allow a single variable type to be stored in an array.

Other languages, such as Python, use lists in place of arrays. List can be resized, and in untyped languages such as Python they can store different data types within the same list.

Arrays in Flowcharts & Pseudocode

The table below lists the flowchart blocks used to represent arrays, as well as the corresponding pseudocode:

Operation Flowchart Pseudocode
Declare Array Declare Array Flowchart Block Declare Array Flowchart Block
ARR = new array[5]
Store Item Store Item in Array Flowchart Block Store Item in Array Flowchart Block
ARR[0] = 5 
Retrieve Item Retrieve Item from Array Flowchart Block Retrieve Item from Array Flowchart Block
X = ARR[0]

Arrays in Java

Let’s review the syntax for working with arrays in Java.

Array Creation

To declare an array in Java, we must give it a type, and a name, with square brackets [] included after the type:

int[] arr;
double[] arr2;

Once the array is declared, we can initialize it using the new keyword, followed by the type and then the size in square brackets []:

arr = new int[5];
arr2 = new double[10];

Of course, we can combine these two statements in to a single statement as well:

int[] arr = new int[5];
double[] arr2 = new double[10];

Finally, if we already know the values which we want to store in the array, we can use a shortcut syntax to initialize the array and place those values directly within it:

int[] arr3 = {1, 2, 3, 4, 5};

Accessing Array Elements

Once the array is created, we can access individual items in the array by placing the index in square brackets [] after the array’s variable name:

x = arr[2];
arr[1] = 5;

Multidimensional Arrays

Java arrays can also be created with multiple dimensions, simply by adding additional square brackets [] to represent and access items in each dimension:

int[][] twoDimArray = new int[5][10];
twoDimArray[0][1] = 5;
int y = twoDimArray[4][9];

Array Operations

There are several operations that can be performed on arrays in Java as well:

int[] arr = new arr[5];

// array length
int len = arr.length;

// copy array
int[] newArr = new arr[5];
// System.arraycopy(source, sourcePosition, destination, destinationPosition, length)
System.arraycopy(arr, 0, newArr, 0, 5);

Array Loops

Finally, we can use a special form of loop, called an Enhanced For loop, to iterate through items in an array in Java:

int[] arr = {1, 2, 3, 4, 5};

for(int i : arr){

Once important thing to note is that arrays accessed within an Enhanced For loop are read only. So, we cannot change the values stored in the array using this loop, but we can access them. If we want to change them, we should use a standard For loop to iterate through the indices of the array:

int[] arr = {1, 2, 3, 4, 5};

for(int i = 0; i < arr.length; i++){
    arr[i] = arr[i] + 5;