course content

Course Content

Introduction to Java

Arrays 2/2Arrays 2/2

Find the length of an Array

The array's length is the number of elements an array can store. You can use the .length property to find the number of elements in the array:

java

Main.java

Loop through an Array

A common way to loop through an array is using a for loop (we will learn this loop in the next chapter). The for loop will iterate through each element in the array and perform the given operations. Here is an example:

java

Main.java

Multidimensional Arrays

Multidimensional arrays are also called rectangular arrays in Java. They are often used to store data in a tabular format. The two-dimensional array is a simple case of a multidimensional array in Java.

It can be declared as follows:

The code for creating a two-dimensional array:

Declaration and creation can be combined as the following code:

java

Main.java

Following is the 2-D array creation for the above code:

Column/RowColumn 1Column 2Column 3Column 4
Row 1a[0][0]a[0][1]a[0][2]a[0][3]
Row 2a[1][0]a[1][1]a[1][2]a[1][3]
Row 3a[2][0]a[2][1]a[2][2]a[2][3]
java

Main.java

question-icon

Retrieve the second element from array:

Select the correct answer

Section 1.

Chapter 4