A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"Hi, Amigo!"

"Hey, Ellie!"

"Today, I'll tell you about a new and interesting entity: arrays. An array is a data type that can store several values instead of just one."

Arrays - 1

"Let's start with an analogy. Let's compare a house and an apartment building. An ordinary house is usually occupied by just one family, but an apartment building is divided into many apartments. To send a letter to a family living in a house, you need to indicate the unique address of the house. To send a letter to a family living in an apartment building, you need to write the apartment building's unique address and the apartment number."

"Everything seems clear so far."

"An array variable is like an apartment-building variable. You can store many values in it instead of just one. Such a variable has several apartments (elements) that you can refer to using an apartment number (index). To do this, indicate the index of the array element you want to access in square brackets after the name of the array variable. It's quite simple."

"I hope so, Ellie."

"An apartment-building variable (array variable) can contain elements of any type. You just need to write 'TypeName[] variable_name' instead of 'TypeName variable_name'."

Here are some examples:

Code Description
String[] list = new String[5];
Create a String array with 5 elements
System.out.println(list[0]);
System.out.println(list[1]);
System.out.println(list[2]);
System.out.println(list[3]);
System.out.println(list[4]);
Five 'null' values will be displayed.

To access the value of a particular array element, use square brackets and the element's index.

int listCount = list.length;
listCount will be assigned the value 5, which is the number of elements in the list array.
list.length stores the array's length (number of elements).
list[1] = "Mom";
String s = list[1];
When assigning objects to array elements, you need to indicate the element index in square brackets.
for (int i = 0; i < list.length; i++)
{
     System.out.println(list[i]);
}
Display the values of all array elements on the screen.

"How interesting!"

"An array variable requires additional initialization."

— ?

"With a regular variable, you can just declare it and then assign various values to it. With an array, it's a bit more complicated."

"You must first create a container that will hold N elements, and only then can you start placing values into the container."

Code Description
String[] list = null;
The list array variable is null. It can only store a reference to a container for elements. You must create the container separately.
String[] list = new String[5];
Create a container for 5 elements and assign a reference to the list variable. This container has 5 apartments (elements) numbered 0, 1, 2, 3, and 4.
String[] list = new String[1];
Create a container for 1 element and assign a reference to the list variable. To put something into this container, we would write something like list[0] = "Yo!";
String[] list = new String[0];
Create a container for 0 elements and assign a reference to the list variable. You can't store anything in this container.

"I see. Now it's getting clearer."

"Here are some basic facts about arrays:"

1) An array consists of many elements.

2) To access a certain element, you indicate its number (index).

3) All elements are of the same type.

4) The initial value for all elements is null; for arrays of primitive types, the initial value is 0, 0.0 (for fractional numbers), or false (for booleans). It's exactly the same as with uninitialized variables that are not in arrays.

5) String[] list simply declares a variable. You need to first create an array (container), put something into it, and only then use it (see the example below).

6) When we create an array (container) object, we need to indicate its length, or the number of elements. We do this using new TypeName[n];

Arrays - 2

Here are some examples:

Code Description
String s;
String[] list;
s equals null
list equals null
list = new String[10];
int n = list.length;
The list variable stores a reference to an object – a 10-element array of Strings
n equals 10
list = new String[0];
Now list contains a 0 element array. The array exists, but it can't store Strings.
list = null;
System.out.println(list[1]);
This will throw an exception (run-time error) and the program will be abnormally terminated: list contains a null reference.
list = new String[10];
System.out.println(list[11]);
This will throw an exception (run-time error): array index out of bounds.

If list contains 10 elements, the valid indices are: 0,1,2,3,4,5,6,7,8, and 9 (for a total of 10 indices).