Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende ¿Qué es Array? | Arrays
Principios Básicos de Java
course content

Contenido del Curso

Principios Básicos de Java

Principios Básicos de Java

1. Iniciando
2. Tipos Básicos, Operaciones
3. Loops
4. Arrays
5. String

book
¿Qué es 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.

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

java

Main

copy
1
dataType[] arrayName = new dataType[capacity];

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

copy
12345678
package com.example; public class Main { public static void main(String[] args) { int[] intArray = new int[5]; // Declares a new int array with a length of 5 System.out.println(intArray.length); // Prints the size of the array } }

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

copy
123456789
package com.example; public class Main { public static void main(String[] args) { int[] intArray = new int[5]; // Declares a new int array with a length of 5 int length = intArray.length; // Declares a new int with the value of the array's size System.out.println(length); // Prints the size of the array } }

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

Main

copy
1234567891011121314151617181920
package com.example; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] array = {5, 2, 9, 1, 6}; // Display the size of the array System.out.println("Array size: " + array.length); // Sort the array Arrays.sort(array); System.out.println("Sorted array: " + Arrays.toString(array)); // Fill the array with the value 0 Arrays.fill(array, 0); System.out.println("Array after filling: " + Arrays.toString(array)); } }

First, it sorts the array in ascending order using Arrays.sort() and displays the sorted array using Arrays.toString(), which converts the array into a readable string format. Then, all elements of the array are replaced with 0 using Arrays.fill(), and the updated array is printed in the same readable format.

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:

If we have an array with 10 elements, the first element will be stored at index 0, and the last one at index 9, because indexing starts from zero.

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

copy
12345678910111213141516
package com.example; public class Main { public static void main(String[] args) { // Declares a new int array with a length of 5 int[] intArray = new int[5]; // Fills the array with numbers from 1 to 5 intArray[0] = 1; intArray[1] = 2; intArray[2] = 3; intArray[3] = 4; intArray[4] = 5; // Prints the fourth element of the array System.out.println(intArray[3]); } }

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

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { // Declares a new pre-filled int array with a length of 5 int[] intArray = {1, 2, 3, 4, 5}; // Prints the first element of the array System.out.println(intArray[0]); } }

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.

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?

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

Selecciona la respuesta correcta

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

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 1
We're sorry to hear that something went wrong. What happened?
some-alt