Back to: Java Tutorials
Arrays are an essential part of Java programming. They allow you to store and manipulate multiple values in a single variable. In this tutorial, we’ll cover the basics of how to use arrays in Java.
- Declaring an Array: To declare an array in Java, you need to specify the data type of the elements in the array and the size of the array. Here’s an example of how to declare an array of integers with a size of 5:
int[] myArray = new int[5];
- Initializing an Array: After declaring an array, we can initialize it with values. We can do this in two ways: by specifying the values when you declare the array or by assigning the values later. Here’s an example of how to initialize an array with values:
int[] myArray = {1, 2, 3, 4, 5};
Alternatively, we can also initialize the array with values using a loop:
int[] myArray = new int[5];
for (int i = 0; i < myArray.length; i++) {
myArray[i] = i + 1;
}
- Accessing Array Elements: We can access individual elements of an array by using an index. The index starts at 0, which means the first element is at index 0, the second element is at index 1, and so on. Here’s an example of how to access an element in an array:
int[] myArray = {1, 2, 3, 4, 5};
int firstElement = myArray[0];
int thirdElement = myArray[2];
- Modifying Array Elements: We can modify the values of individual elements of an array by using an index. Here’s an example of how to modify an element in an array:
int[] myArray = {1, 2, 3, 4, 5};
myArray[0] = 10;
myArray[2] = 30;
- Array Length: We can get the length of an array using the
length
property. Here’s an example:
int[] myArray = {1, 2, 3, 4, 5};
int arrayLength = myArray.length;
- Printing Elements of a Java Array:
To print the contents of an array in Java, we can use a loop to iterate through each element in the array and print it. Here’s an example of how to print the contents of an integer array:
int[] myArray = {1, 2, 3, 4, 5};
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
This code uses a for loop to iterate through each element in the array, and the System.out.print()
method is used to print the value of each element, followed by a space.
We can also use the Arrays.toString()
method to print the contents of an array. Here’s an example:
int[] myArray = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(myArray));
This code uses the Arrays.toString()
method to convert the array to a string representation, which is then printed using the System.out.println()
method. This method is useful for printing the entire array at once, but it may not be as flexible as using a loop if you need to manipulate or format the output in a specific way.
Multidimensional Arrays:
In addition to one-dimensional arrays, Java also supports multidimensional arrays, which are arrays that have more than one index. For example, a two-dimensional array can be used to store a table of values with rows and columns.
int[][] myArray = {{1, 2, 3}, {4, 5, 6}};
int firstElement = myArray[0][0];
int thirdElement = myArray[1][2];
Arrays as Parameters:
We can pass arrays as parameters to methods in Java. Here’s an example of a method that takes an array as a parameter and returns the sum of its elements:
public static int sumArray(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
To call this method, we can pass an array as a parameter:
int[] myArray = {1, 2, 3, 4, 5};
int sum = sumArray(myArray);
Here’s a complete example of using arrays in Java:
import java.util.Arrays;
public class ArrayExample {
public static void main(String[] args) {
// Declaring and initializing an array
int[] myArray = {1, 2, 3, 4, 5};
// Accessing array elements
int firstElement = myArray[0];
int thirdElement = myArray[2];
// Modifying array elements
myArray[0] = 10;
myArray[2] = 30;
// Printing array contents using a loop
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + " ");
}
// Printing array contents using Arrays.toString()
System.out.println(Arrays.toString(myArray));
// Getting array length
int arrayLength = myArray.length;
// Declaring and initializing a 2D array
int[][] my2DArray = {{1, 2, 3}, {4, 5, 6}};
// Accessing 2D array elements
int firstElement2D = my2DArray[0][0];
int thirdElement2D = my2DArray[1][2];
// Printing 2D array contents using nested loops
for (int i = 0; i < my2DArray.length; i++) {
for (int j = 0; j < my2DArray[i].length; j++) {
System.out.print(my2DArray[i][j] + " ");
}
System.out.println();
}
// Passing an array as a parameter to a method
int sum = sumArray(myArray);
System.out.println("Sum of array elements: " + sum);
}
public static int sumArray(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
}
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author