Arrays

Module 1. Java Syntax
Level 7 , Lesson 0
Available

1. An array is a container of elements

You've probably heard that computers can process huge amounts of information. Of course, conditional statements (if-else) and loops (for, while) are a big help here. But they can only take you so far. After all, the data you process needs to be stored somehow.

Like almost all programming languages, Java facilitates data processing by providing this great thing called an array (Array class). They are also sometimes called tables.

An array is a special object that lets you store not one value, but several.

Java Arrays

Earlier we compared a variable to a box (in which you can store any value). Continuing that analogy, we can think of an array as a box that has internal compartments. Each compartment in the "box" (array) has a number. Of course, the numbering starts from zero...

Or we can make another analogy. Let's compare an ordinary house and a high-rise apartment building. An ordinary house is occupied by a single family, but a high-rise apartment building is divided into apartments. If you want to send a letter to a family that lives in an ordinary house, you indicate the unique address of the house. And to send a letter to a family that lives in an apartment, you indicate the unique address of the building as well as the apartment number.

An array variable is like a high-rise variable. It can store not one but many values. Such a variable has several apartments (cells). Each of them can be addressed by their number (index).

To do this, after the name of the variable, you indicate the index of the cell you want to address, wrapping the index in square brackets. This is pretty simple:

array[index] = value;

Where array is the name of the array variable, index is the cell number in the array, and value is the value that we want to put into the specified cell.

But to start, let's out how to create arrays.


2. Creating an array of elements in Java

Creating an array of elements in Java

Let's say your program needs to store 100 integers somewhere. An array would be a good candidate for this. And how do you create one?

If we wanted to store a single integer, the int type would suit us. But if we want to store 100 integers, we probably need an array of ints. This is how the code to create one would look:

int[] array = new int[100];

Let's explore this statement.

As you might have guessed, to the left of the equal sign we have the declaration of a variable named array whose type is int[]. The int type is followed by square brackets, which hints that "boxes" of this type can store not one but several values.

To the right of the equal sign, we have an example of "object creation" (the new keyword) to get 100 elements (cells) whose type is int. Nothing too difficult here.

Similarly, if we wanted to create an array of 20 cells to store real numbers, then our code would look something like this:

double[] vals = new double[20];

The number of cells in an array is called the size of the array or the length of the array. And because arrays can store many values, they are also called containers.

Here's an important fact: you cannot change the size of an array after it is created.

You can create a new one, but the length of the existing container cannot be changed.



3. Working with the cells of an array

Working with the cells of an array

Okay, we've learned how to create arrays. Now how do we work with them?

Well, in almost the same way as with ordinary variables. The only difference is that after the name of the array variable, we have to indicate the number of the cell that we are working with.

The numbering of cells in an array always starts from zero. If we have an array of 10 elements, then the numbers (indices) of its cells are 0..9. If the array contains 200 elements, then the indices are 0..199. And so on by analogy.

Examples:

Code Explanation
int[] a = new int[10];
a[2] = 4;
a[7] = 9;
a[9] = a[2] + a[5];
Create an array of 10 int elements.
Assign the value 4 to the cell with index 2.
Assign the value 9 to the cell with index 7.
In the cell with index 9, write the sum of the values that are stored in cells 2 (which stores the value 4) and 5 (which stores the value 0).

This is what will be stored in memory after this code is executed:

Working with the cells of an int array 2

The column on the left (in gray) represents the cell numbers (indices). The cells store the assigned values: 4, 9 and 4. When the array is created its cells are all filled with zeros.

This is important. All cells in an array have the same data type. If we create an array of Strings, only strings can be stored in its cells. An array's data type is specified when it is created. Neither the data type nor the length of the array can be changed later.


Comments (13)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Evgeniia Shabaeva Level 32, Budapest, Hungary
18 March 2024
It's interesting that on my IntelliJ IDEA the tasks are unavailable while I can open them here, on the website. I wonder what I need to do to help them sync.
Grzesiek Level 9, Poland
29 March 2024
Just click on your profile picture (IntelliJ) and synchronize the tasks.
haalk3n Level 6, Romania
14 February 2024
For the exercise "Working with elements of an array", please for the love of God make the instructions clear and concise. I've read 9 times through it and I had to get 2 more people to finally figure WHAT IT WANTS ME TO DO. English Native speakers here.
AntonisTaramigkos Level 6, Greece
9 January 2024

for (int i = 0; i < array.length; i++) {
            if(i % 2 == 0){
                array[i] *= (-1);
            }
Andres Rodriguez Level 5, Mesa, United States
23 November 2023
for(int i = 0; i < array.length; i++){ if (array[i] % 2 == 0){ array[i] = array[i] * -1;
Shanthi Sri Level 64, CodeGym University in India, India Expert
7 September 2023
using if class
Shanthi Sri Level 64, CodeGym University in India, India Expert
7 September 2023
help me
NonEuclid Level 20, Zurich, Switzerland
17 August 2022
In the last task I did a loop that gives me the same answer but it says it is wrong
Attila Kiss Level 8, Hungary
21 September 2022
Same happened to me too. Using a for loop is a better solution imao.
Anonymous #11201052 Level 7, Canada
10 December 2022
You transform the number to a negative only if it's an even number. I did a for loop but forgot to verify if it's a even number before transforming it to a negative number.
Parsa Level 62, Bangalore, India Expert
14 October 2023
I think there was a problem with your code. I also did a for loop, but it was accepted.
Rene Level 28, Netherlands
30 October 2023
Same here. for-loop used and was accepted. Just let it check your array from start to end and flip valuesign when its even.
Anar Hasanov Level 23, Baku, Azerbaijan Expert
8 November 2023
Looking for the previous task named-"Even and odd cells of an array" u used arrays index in for-loop. But in the condition of task-"Working with elements of an array" we see that we have to use ELEMENTS of given array. We have to be more attentive to small details. 💪