Java Arrays

FacebooktwittertumblrFacebooktwittertumblr
array-magic_vertex-academy
What Is A Java Array?

In our article "Variable types in Java. Creation of variables" we said that a variable is a container that stores a value which will later be used in the program.

So:

array-concept_vertex-academy
  • an ordinary variable can store only one value.
  • an array is a simple variable which can contain multiple values (unlike an ordinary variable, which contains only one). An array is a sort-of “magical” container in which we can place multiple values (“boxes”).

Below are some examples of arrays:

Example № 1

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

Example № 2

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


Every Array Has A Length

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

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

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

What lengths do the following three arrays have?

Answers:

massiv3_vertex-academy

The length of this array is 6, because there are six numbers in it: 1, 8, 15, 30, 2, and 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, and Odessa.

How Can I Get The Length Of An Array Automatically?

Well, you can simply count the numbers one at a time. But what if there are a lot of elements? You probably don't have the patience to count 3000 numbers. So to count the numbers automatically, we need to use an array length property, since it contains the number we want: the array length. To do this, write the following:
array name +.+length // The plus sign means combination.

For example, we have the following array:  

We write int k = array1.length; .

As a result, the variable k is created and it will contain the value 3.

You can run this code to see it in practice:


In Each Array, The Element Values Are Stored Under The Serial Numbers (the Indices)

By the way, the numbers start from “zero.” Take a look at the examples below:

Example № 1

In this array:

5 is an element with index number 0.

17 is an element with index number 1.

350 is an element with index number 2.

Example № 2

In this array:

“Java” is an element with index number 0.

“is the best” is an element with index number 1.

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

So the index number of an element is one less than you'd expect it to be (the first element has index number 0, the second has index number 1, etc.)

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

Why Are The Element Values In Arrays Stored Under Serial Numbers (Indices)?

The answer is very simple: so that you can access specific array elements by index number. Chaotically putting a lot of small boxes in a big one wouldn't do much good if we need to be able to find a small box which has the information we want. That’s why everything that we put inside a big box has a serial number.

To get what is stored in a small box, we need to access the box by index number. To do this, we write the variable name (the big box) and then, in square brackets and no spaces, we write the index number of the small box,

array[number]

Let’s see how it works in practice.

  1. How do we access the element of array1 that has index number 0? Answer: array1[0]
  2. How do we access the element of array1 that has index number 1? Answer: array1[1]
  3. How do we access the element of array1 that has index number 2? Answer: array1[2]

How Do I Create An Empty Array?

array1_vertex-academy

In all the previous examples, we created arrays and immediately initialized them with some values. However, it's possible to create an array of the size you want and later give it values (if you don't know what values you will store in it when you create it). For example, a doctor tells a woman that she will have twins, but he doesn't know what genders the twins will have. This is an example of an array of two elements of the “child” type, in which the values are not yet known.

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

type [] name = new type[size];

Please note that for primitive types, the type before the equals sign and after the equals sign must be the same!

In the future, we will see that this rule can be violated, but we'll cross that bridge when we get to it.

It should look as follows in the program:

Let’s take a deeper look at this line of code:

By means of int[] array1  we declared an array of integers and we gave it the name “array1.”

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

Please note: Despite the fact that the array1 is considered to be empty, it in fact still contains values by default. When you create an empty array, 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 will be 0 (i.e. int [] array1 = new int [3] is the same as int [] array1 = {0,0,0}).

How Do I Assign Values To This Type Of An Array?

Run this code on your computer:

With the help of these lines, we access each element of the array and assign them with the necessary values:

So if array1[0] is standing to the right of the equal sign, we get its value. However, if it is standing to the left, we assign a value to it.

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

In the console, you will see the following:

5

17

350

One More Important Thing: After You Create An Array, The Size Of The Array Cannot Be Changed

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

You probably think that this code helped us create an array. But actually, no, it didn't. How is that so? After all, we:

  • indicated the array, by means of []
  • specified that the array is integer-valued, by means of int
  • named the array "array1"

But an array wasn’t created.

An array is created only when you allocate memory for it. And memory is allocated only when you do this:

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

or this:

You cannot add elements in an array higher than the indicated quantity. Empty slots will store the value “0.” In Java, there are other tools that allow you to create limitless lists of elements. You will learn about them a bit later.


Let’s Summarize:

1. An array is a variable which can contain multiple values (instead of just one value).

2. All elements of an array are the same type.

3. If you create an empty array, values will be assigned to variables depending on the array data type.

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

4. After you create an array, the size of the array cannot be changed.

FacebooktwittertumblrFacebooktwittertumblr

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