In this lab, you will learn how to work with arrays in Java. An array is a data structure that allows you to store multiple values of the same data type in a single variable. Arrays are useful for managing collections of data such as lists of numbers, names, or other objects. You will create, initialize, and manipulate arrays in this lab, exploring how to work with both single-dimensional and multi-dimensional arrays.
By the end of this lab, you will be able to:
cd ~
ArrayExample.java
:nano ArrayExample.java
public class ArrayExample {
public static void main(String[] args) {
// Declare an array of integers
int[] numbers = {1, 2, 3, 4, 5};
// Access and print elements of the array
System.out.println("First element: " + numbers[0]);
System.out.println("Second element: " + numbers[1]);
System.out.println("Third element: " + numbers[2]);
// Modify an element in the array
numbers[2] = 10;
System.out.println("Updated third element: " + numbers[2]);
// Use a for loop to iterate over the array
System.out.println("\nAll elements in the array:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
CTRL + O
, then hit Enter
to confirm the filename.CTRL + X
.javac
command:javac ArrayExample.java
ArrayExample.class
. Verify this by running:ls
java
command:java ArrayExample
First element: 1
Second element: 2
Third element: 3
Updated third element: 10
All elements in the array:
1
2
10
4
5
ArrayExample.java
file again using Nano:nano ArrayExample.java
import java.util.Scanner;
public class ArrayExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Create an array of 5 integers
int[] numbers = new int[5];
// Get user input to fill the array
System.out.println("Enter 5 numbers:");
for (int i = 0; i < numbers.length; i++) {
System.out.print("Number " + (i+1) + ": ");
numbers[i] = scanner.nextInt();
}
// Print the array elements
System.out.println("\nYou entered:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
javac ArrayExample.java
java ArrayExample
// Create a 2D array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Print elements of the 2D array
System.out.println("\n2D Array (Matrix):");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
javac ArrayExample.java
java ArrayExample
// Find the maximum value in the array
int max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
System.out.println("\nMaximum value in the array: " + max);
.class
files:rm *.class
In this lab, you learned how to create, initialize, and manipulate arrays in Java. You explored both single-dimensional and multi-dimensional arrays, accessed and modified array elements, and used loops to iterate through arrays. Arrays are a powerful way to store and manage multiple values in a program, making them a fundamental tool in any developer's toolkit.