java / beginner
Snippet
Accessing Array Elements
Arrays store multiple items in a single variable. You can retrieve a specific item using its index number, which always starts at 0 for the first element.
snippet.java
1
2
String[] names = {"Alice", "Bob"};String first = names[0];
Breakdown
1
String[] names = {"Alice", "Bob"};
Defines an array of Strings with two predefined values.
2
String first = names[0];
Accesses the first element of the array using index 0 and stores it in 'first'.