Java arrays

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr
array-magic_vertex-academy
What is Java array?

In our article "Variable types in Java. Creation of variables" we said that a variable - is a container, that can store a value for a further usage in a program.

Well, then:

array-concept_vertex-academy
  • an usual variable can store only one value.
  • массив - это простая переменная, которая может содержать не an array is a usual variable which can contain multiple values (in contrast to the usual variable, which contains only one value). The array is sort of a “magical” container in which we can put multiple values (“boxes”).

Below are examples of arrays:

Example №1

As you can see, we created an array of integers, which we called array1 and assigned three numbers to it: 5, 17, 350

Example №2

As you can see, we created an array of strings, which we called array2 and assigned a word “Java” and a phrase “is the best” to it. And we wrote them in double quotes, because this is an array of the string type.


Each array has a length

For example, the length of this array equals 3, because there are three numbers in it: 5, 17, 350

And the length of this array equals 2, because there are two values in it: “Java” and “is the best”.

To put it simply, the length of an array is the number of elements that are contained in it (the number of small boxes inside big one).

Now we suggest you to answer, which length do the following three arrays have?

Right answers:

massiv3_vertex-academy

The length of this array is 6, because there are six numbers in it: 1, 8, 15, 30, 2, 3.

massiv4_vertex-academy

The length of this array is 2, because there are two numbers in it: 1 and 3.

The length of this array is 4, because there are four words in it: Kyiv, Lviv, Lutsk, Odesa.

And how do I automatically get the length of an array?

For sure you can simply count the number in a manual way. But what if there are too many elements and it’s difficult to count them in head? It is unlikely that you have the patience to count 3000 numbers. To do this, you need to use an array length property. It contains our desired number – the array length. In order to get it, you need to write the following:
array name +.+length // “plus” means combination, we just put it away.

For example, we have the following array.  

We write int k = array1.length; .

In the result we create the variable k, which will contain the value 3.

You can try to run this code in order to see this in practice:


In any array the element values are stored under the serial numbers (indexes)

What is more, the numbering starts at “zero”. We suggest you to check the examples below:

Example №1

In this array:

5 is an element with index 0

17 is an element with index 1

350 is an element with index 2

Example №2

In this array:

“Java” is an element with index 0

“is the best” is an element with index 1

If you are very fond of sweets, you can imagine an array as macarons.

That is, the index of element equals its serial number -1.

As we can see, the length of the macaron array equals 3. The same logic applies to the array serial numbers (indexes).

Let’s understand why the element values in arrays are stored under the serial numbers (indexes)?

The answer is very easy – to be able to access specific array elements by index. Chaotically putting a lot of small boxes in the big one won’t do much good, that’s why we need to know where is the small box which contains the information we need. That’s why everything what we put inside the big box has the serial number.

In order to get what is stored in the small box, we need to access it by index. To do it we write the variable name (the big box) and then the index of the box within square brackets without spaces.

array[number]

Let’s see how it works in practice.

  1. How do we access an element of array1 at index 0? Answer: array1[0]
  2. How do we access an element of array1 at index 1? Answer: array1[1]
  3. How do we access an element of array1 at index 2? Answer: array1[2]

How do I create an empty array?

array1_vertex-academy

In all previous examples we created the arrays and immediately initialized them with some values. However, there is a possibility to create an array of the desired size in the first place and only then to fill it with values (if during the creation we don’t know, which values will be stored in it). For example, a doctor tells a woman that she will have twins, but the sex of the children isn’t known yet. Here’s an array of two elements of “child” type, but the values are not known yet.

To create an array of this type, you need to use the key word new. Now the process of array creation looks as follows:

type [] name = new type[size];

Please note, that if we speak about an array of primitive types, the type before = and after = should stay the same!

In the future we will see, that this rule can be violated, but this is another story.

In the program in would look as follows:

Let’s analyze this code line:

By means of int[] array1  we declared the array of integers with the name “array1”

By means of = new int[3]; we created an empty array, which consists of three elements.

Please note: Despite the fact that the array “array1” is considered to be empty, in fact it still contains the values by default… If you create an empty array, the values are assigned to variables according to the array data type:

  • for int - 0
  • for float, double - 0.0
  • for String - null value
  • for char - \0
  • дforя boolean - false value

Since the array in the example above is integer-valued all three elements equals 0. I.e. int [] array1 = new int [3]; is the same as int [] array1 = {0,0,0}

And how do fill this type of arrays with values?

Try to run this code on your computer:

With the help of this lines we access each element of the array and fill it with needed value:

That is, if array1[0] stays to the right of equal sign we get its value, but if it stays to the left – we assign the value to it.

We added these lines in order to show you that each array element got values 5, 17, 350 respectively.

In the console you will see the following:

5

17

350

Another important thing is that you cannot change the size of created array.

Let’s take a look at the following code line:

You probably think, that by means of this code line we created an array. But the answer is no. How so? After all, we:

  • by means of [] indicated the array
  • by means of int specified that the array is integer-valued
  • specified the name of the array – array1

But the array wasn’t created.

An array is created when you allocate memory for it. And the memory is allocated for an array only when preforming the following step:

i.e. when we write  = new int [3];

or the following one:

You cannot add elements in an array above the specified limit, and empty slots will store the value “0”. In Java there are other tools that allow you to create non-dimensional lists of elements – you will learn about them a little bit later.


Let’s summarize:

1. An array is a variable, which can contain multiple values instead of only one value.

2. All array elements have the same type.

3. If you create an empty array the values are assigned to variables according to the array data type.

  • for int - 0
  • for float, double - 0.0
  • for String - null value
  • for char - \0
  • for boolean - false value

4. You cannot change the size of created array.

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Publiction author

offline 5 hours

Vertex Academy

Comments: 0Publics: 274Registration: 06-03-2016

Subscribe news

Subscribe to our newsletter we will keep you up-to-date!

Самоучители--узнать детальнее--