Arrays
Store multiple values in one variable. Learn about Single and Multi-Dimensional Arrays.
Store multiple values in one variable. Learn about Single and Multi-Dimensional Arrays. This hands-on tutorial focuses on practical implementation of arrays concepts.
Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
Declaring an Array
String[] cars; // Preferred way
String cars[]; // Also valid, but less common
String[] cars = {"Volvo", "BMW", "Ford"};
int[] myNum = {10, 20, 30, 40};
Accessing Elements
You access an array element by referring to the index number. Indexes start with 0.
System.out.println(cars[0]); // Outputs "Volvo"
Changing an Element
cars[0] = "Opel";
Loop Through an Array
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
Multidimensional Arrays
A multidimensional array is an array of arrays.
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
int x = myNumbers[1][2]; // 7
Interactive Code
Work with arrays!
AI Mentor
Confused about "Java Arrays: declaration, initialization, indexing and multidimensional arrays"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3What is the index of the first element in an array?
Next Steps
Primitive types are efficient, but sometimes we need Objects. Enter Wrapper Classes.