Arrays
In the world of programming, arrays are one of the most fundamental data structures. They provide a way to store and manipulate a fixed-size sequence of elements of the same type.
As a senior engineer with experience in Java, Spring Boot, and MySQL, you have likely encountered arrays in your coding journey. An array can be thought of as a collection of variables of the same type, where each variable, referred to as an element, is identified by its index.
Arrays in Java are zero-indexed, meaning the first element in the array has an index of 0, the second element has an index of 1, and so on.
TEXT/X-JAVA
1// Creating and initializing an array
2int[] numbers = {1, 2, 3, 4, 5};
3
4// Accessing elements of an array
5System.out.println("The first element is: " + numbers[0]);
6System.out.println("The last element is: " + numbers[numbers.length - 1]);
7
8// Updating an element of an array
9numbers[2] = 6;
10System.out.println("The updated element at index 2 is: " + numbers[2]);
11
12// Iterating over an array
13for (int i = 0; i < numbers.length; i++) {
14 System.out.println("Element at index " + i + ": " + numbers[i]);
15}
xxxxxxxxxx
19
class Main {
public static void main(String[] args) {
// replace with your Java logic here
int[] numbers = {1, 2, 3, 4, 5};
// Accessing elements of an array
System.out.println("The first element is: " + numbers[0]);
System.out.println("The last element is: " + numbers[numbers.length - 1]);
// Updating an element of an array
numbers[2] = 6;
System.out.println("The updated element at index 2 is: " + numbers[2]);
// Iterating over an array
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment