Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is Array? | Arrays
Java Basics

What is Array?What is Array?

Arrays

Storing information exclusively in variables isn't always convenient, as it would consume a significant amount of stack memory and require a substantial number of code lines.

The solution to this problem is arrays.

An array is a container that can hold a fixed number of objects of the same type.

Let's explore the basic syntax for declaring an array in Java:

java

Main.java

We can use any data type when declaring an array, including int, float, char, and more. You can even use your custom classes in array declarations. Learning how to create your own classes and their objects will be covered in a separate Java Extended course.

It is mandatory to specify the size of the array in square brackets ([]). Let's consider an example of creating an array of type int with a size of 5:

java

Main.java

As you may have noticed, we used something unfamiliar in conjunction with the console output. This thing is called a array's property.

Arrays have their own properties. Properties are called using dot (.) notation on the object with them. In our example, the intArray array has properties, and we are using one of them. The .length propertie returns the size (length) of the array as an int value. This can also be written differently:

java

Main.java

Now, let's examine three fundamental methods and properties for working with arrays:

  • array.length; - Returns the size of the array;
  • Arrays.sort(array); - Sorts the elements of the array in ascending order;
  • Arrays.fill(array, value); - Sets all elements of the array to the specified value.

Note

array is simply an example name for the array. You can name the array as you like, depending on the task requirements or your preferences.

How to Access Specific Data in an Array?

Arrays in Java are indexed. Each array element is situated in its cell and assigned a unique ID. The numbering of elements within the array begins at 0. Suppose we have an array of numbers from zero to nine, so it has 10 elements in it.

Let's examine a diagram that illustrates how the elements are organized within the array:

Note

It's worth highlighting the formula N-1, where N represents the element's number in the array, and N-1 denotes its position in the array. This method allows us to calculate the element's position in the array effortlessly. This concept is known as zero-based indexing.

Practice is always better than just theory, so let's explore an example of adding and removing elements from an array using indices:

java

Main.java

We have populated our array with elements from one to five by specifying each element with an index. However, this approach lacks elegance and requires many lines of code. Let's explore an alternative method of declaring an array that comes pre-filled:

java

Main.java

We created an array identical to the one we made earlier, but this time, it required fewer lines of code and appeared more elegant.

It's important to note that when using this array notation, the elements must be enclosed within curly braces ({}) and separated by commas.

Note

Notice that when declaring an array in this manner, we don't specify its size explicitly. The compiler automatically determines the required length of the array based on the provided elements and populates it accordingly.

1. What will be printed when we'll execute this code?
2. What will be printed when we'll execute this code?

What will be printed when we'll execute this code?

Select the correct answer

What will be printed when we'll execute this code?

Select the correct answer

Everything was clear?

Section 4. Chapter 1
course content

Course Content

Java Basics

What is Array?What is Array?

Arrays

Storing information exclusively in variables isn't always convenient, as it would consume a significant amount of stack memory and require a substantial number of code lines.

The solution to this problem is arrays.

An array is a container that can hold a fixed number of objects of the same type.

Let's explore the basic syntax for declaring an array in Java:

java

Main.java

We can use any data type when declaring an array, including int, float, char, and more. You can even use your custom classes in array declarations. Learning how to create your own classes and their objects will be covered in a separate Java Extended course.

It is mandatory to specify the size of the array in square brackets ([]). Let's consider an example of creating an array of type int with a size of 5:

java

Main.java

As you may have noticed, we used something unfamiliar in conjunction with the console output. This thing is called a array's property.

Arrays have their own properties. Properties are called using dot (.) notation on the object with them. In our example, the intArray array has properties, and we are using one of them. The .length propertie returns the size (length) of the array as an int value. This can also be written differently:

java

Main.java

Now, let's examine three fundamental methods and properties for working with arrays:

  • array.length; - Returns the size of the array;
  • Arrays.sort(array); - Sorts the elements of the array in ascending order;
  • Arrays.fill(array, value); - Sets all elements of the array to the specified value.

Note

array is simply an example name for the array. You can name the array as you like, depending on the task requirements or your preferences.

How to Access Specific Data in an Array?

Arrays in Java are indexed. Each array element is situated in its cell and assigned a unique ID. The numbering of elements within the array begins at 0. Suppose we have an array of numbers from zero to nine, so it has 10 elements in it.

Let's examine a diagram that illustrates how the elements are organized within the array:

Note

It's worth highlighting the formula N-1, where N represents the element's number in the array, and N-1 denotes its position in the array. This method allows us to calculate the element's position in the array effortlessly. This concept is known as zero-based indexing.

Practice is always better than just theory, so let's explore an example of adding and removing elements from an array using indices:

java

Main.java

We have populated our array with elements from one to five by specifying each element with an index. However, this approach lacks elegance and requires many lines of code. Let's explore an alternative method of declaring an array that comes pre-filled:

java

Main.java

We created an array identical to the one we made earlier, but this time, it required fewer lines of code and appeared more elegant.

It's important to note that when using this array notation, the elements must be enclosed within curly braces ({}) and separated by commas.

Note

Notice that when declaring an array in this manner, we don't specify its size explicitly. The compiler automatically determines the required length of the array based on the provided elements and populates it accordingly.

1. What will be printed when we'll execute this code?
2. What will be printed when we'll execute this code?

What will be printed when we'll execute this code?

Select the correct answer

What will be printed when we'll execute this code?

Select the correct answer

Everything was clear?

Section 4. Chapter 1
some-alt