4.6 Basic Java Syntax: Arrays

In Java, an array is a collection of elements of the same type stored in contiguous memory positions. Arrays are used to store multiple values ​​in a single variable, rather than declaring separate variables for each value. To understand and use arrays in Java, it is essential to understand their syntax and how they work.

Array Declaration

To declare an array in Java, you must specify the data type of the elements and the number of elements that the array will store. The syntax for declaring an array is:

type[] ArrayName;

For example, to declare an array of integers, you would use:

int[] myArray;

Array Initialization

After declaring an array, you must initialize it before using it. There are several ways to initialize an array in Java:

Static Initialization

One way to initialize an array is to provide the values ​​of the elements directly in the declaration. This is known as static initialization. The syntax is:

type[] ArrayName = {value1, value2, value3, ...};

For example:

int[] myArray = {10, 20, 30, 40, 50};

Dynamic Initialization

Another way to initialize an array is to create a new array with the new keyword and specify the size of the array. This is known as dynamic initialization. The syntax is:

ArrayName = new type[size];

For example:

myArray = new int[5];

This creates an array of integers with five elements, all initialized to the default value of 0.

Access to Array Elements

Elements of an array are accessed using indexes. In Java, array indices start with 0. Therefore, the first element of an array is at index 0, the second element is at index 1, and so on. The syntax for accessing an element of an array is:

ArrayName[index];

For example, to access the third element of myArray, you would use:

int element = myArray[2];

Changing Elements of an Array

To modify an element of an array, you simply assign a new value to the element at the desired index. For example:

myArray[2] = 100;

This assigns the value 100 to the third element of the array.

Size of an Array

The size of an array is fixed after it is initialized. To get the size of an array, you can use the length property. For example:

int size = myArray.length;

Multidimensional Arrays

Java supports multidimensional arrays, which are arrays of arrays. The syntax for declaring a two-dimensional array is:

type[][] ArrayName;

For example:

int[][] array = new int[3][4];

This creates an integer matrix with 3 rows and 4 columns.

Iterating Over Arrays

To loop through all the elements of an array, you can use a loop. The for loop is commonly used for this purpose:

for (int i = 0; i < myArray.length; i++) {
    // Access the array element with meuArray[i]
}

Java also offers the for-each loop, which simplifies iterating over arrays:

for (int element: myArray) {
    // Use the element directly
}

Arrays and Exceptions

When working with arrays, it is important to be aware of exceptions that may occur. The ArrayIndexOutOfBoundsException exception is thrown if you try to access an index that is outside the range of the array. Always check that the index is within the bounds of the array to avoid this exception.

Conclusion

Arrays are a fundamental data structure in Java, allowing you to store and manage collections of data efficiently. Understanding the syntax and functioning of arrays is essential for any programmer who wants to create robust and efficient solutions. By mastering the use of arrays, you will be well equipped to deal with a wide range of programming problems.

Now answer the exercise about the content:

_Which of the following correctly describes the way to access the third element of an array called `myArray` in Java?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Basic Java Syntax: Methods (Declaration, Parameters, Return)

Next page of the Free Ebook:

11Basic Java Syntax: Methods (Declaration, Parameters, Return)

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text