Arrays

New Java Syntax
Level 6 , Lesson 1
Available

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).

Comments (30)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Anonymous #10907411 Level 6, United States of America, United States
17 January 2022
I've learned to just skip as much tasks as possible if they are hard because I know I will just be taught later in the course. (annoying) also I feel like code gym is a secondary learning place. I watch videos on youtube first then I come here to learn from reading the reasonings more in depth. It has been super helpful!
abhishe_kira Level 18, India Expert
26 June 2023
Remember one this as they say The more you sweat in practice the less you bleed in wars. So, don't choose the easy way to skip the tasks just do your research on topic from google, YouTube etc. and give it your best shot.
SvenskKung Level 10, United States of America, United States
19 October 2023
This approach is exactly why we see so many people quit around level 6-8. New skills require effort. If you're skipping content, it *will* bite you sooner rather than later.
Gosia Level 6, Germany, Germany
14 January 2024
Actually there is a bigger sense underneath in this approach to learning. Just by exposing first to the task, trying to solve them first, you will remember given part of knowledge better after it is finally presented to you.
Aldo Luna Bueno Level 1, Peru
22 November 2021
Easy! I love the these analogies.
Sinisa Level 11, Banja Luka, Bosnia and Herzegovina
1 March 2021
This should be taught before Classes thing.
Noob_Coder Level 22, Springfield, United States Expert
26 October 2020
Like the style how code gym teaches,it is always harder to know before learning details related to the problem,but that's how u need to learn when u get a job from google!XD
amj Level 7, Imphal , India
3 September 2020
when u explain by giving example such as appartment/home etc , i really understand easily
Oliver Heintz Level 18, Mustang, United States
10 June 2020
So frustrating to learn all this now. I spent hours Googling to solve some of the harder tasks in Level 6 dealing with arrays. I guess I've learned a lot and the process is working, but it is frustrating to have to teach myself something before being taught it.
Nikitata Level 22, Ba Sing Se, United States
7 July 2020
Yes exactly!
9 August 2020
Get used to it, because that's how it is on the job. You will live on Stack Overflow.
Oliver Heintz Level 18, Mustang, United States
18 August 2020
Yeah, I get that... But I also get that I am paying a monthly subscription to learn a skill from this website. Would be nice if THIS website, the one I am PAYING for, would teach me the skill.
gh0stl0nely Level 23, Toronto, Canada
20 August 2020
Agreed. Things won't just be put in front of you like that in the real world. These are basic stuffs that can be googled very easily. The trick is to look at the problem and think of the right question to google.
Maryem Vickers Level 7, HT..., United Kingdom
7 October 2020
Definitely.
Sidd Level 7, Maple Valley, United States
9 June 2020
Good to know that there will be apartments when robots take over the world
HaeWon Chung Level 17, Boston, United States
26 January 2020
Last examples are confusing. Is there any color code of blue or green list? It would be better to use same color unless there is a reason to differentiate them.

list = null;
System.out.println(list[1]);
For me, it's not clear what it is trying to say. This

list = null;
System.out.println(list);
and this

list[1] = null;
System.out.println(list[1]);
run without an error. Why are you trying to reference an index that doesn't exist? In other words, I don't understand what that lesson I can learn from that error.
Nickolas Johnson Level 8, St. Louis, United States of America
21 March 2020
I think it's more helpful to point out that the last element of an array will always be n-1 where n is the size of your array. For example:

list = new String[10]; //this will make an array with 10 slots
System.out.println(list[10]; /*this is won't work because while 
there are 10 elements in the array, the 10th slot is actually
 the number 9 because it starts at zero*/
Gellert Varga Level 23, Szekesfehervar, Hungary
18 April 2020
It doesn't matter if the color of "list" is blue or green. Each example is completely separate, independent of each other. The last and the second last examples just shows us: you can not reference to such an item, wich does not exist. 1) if list=null, then list[1] item is not available. = error. 2) String[] list = new String[5]; System.out.println(list[5]); // ERROR! The list[5] means the non-existent sixth item. (The index numbers are starting with the zero.)
David Graves Level 7, Kokomo, United States
6 December 2019
Very interesting.
Kris Level 9, Englewood, United States
11 November 2019
In the index picture should it not be Element (at index 9) for the blue square ?
Joel Level 7, Alsenberg, Germany
25 November 2019
No, it is the 9th element, but index 8.
Kris Level 9, Englewood, United States
26 November 2019
Thanks Joel, it makes sense now, it's element 9 at index 8 :):):)