java / beginner
Snippet
Working with Arrays
Arrays are used to store multiple values of the same data type in a single variable. You access elements using their index, starting at 0.
snippet.java
1
2
3
int[] numbers = {10, 20, 30};int firstElement = numbers[0];int length = numbers.length;
Breakdown
1
int[] numbers = {10, 20, 30};
Declares and initializes an integer array with three values.
2
int firstElement = numbers[0];
Accesses the first element of the array using index 0.
3
int length = numbers.length;
Uses the length property to find out how many elements are in the array.