Creating Arrays
Creating and Initializing One-Dimensional Arrays:
-
Declaring an array:
int[] myIntArray; // Declares an integer array -
Allocating memory (initializing the array):
myIntArray = new int[5]; // Allocates an array that can store 5 integers -
Assigning array elements:
myIntArray[0] = 10; myIntArray[1] = 20; myIntArray[2] = 30; myIntArray[3] = 40; myIntArray[4] = 50; -
Initializing an array with a loop:
for (int i = 0; i < myIntArray.length; i++) { myIntArray[i] = i * 10; // Initializes each element to i * 10 } -
Creating and initializing an array (at declaration):
int[] myInitializedArray = {10, 20, 30, 40, 50};
Creating and Initializing Multi-Dimensional Arrays:
-
Declaring a two-dimensional array:
int[][] my2DArray; -
Allocating memory:
my2DArray = new int[3][2]; // Allocates a 3x2 two-dimensional array -
Assigning elements to a two-dimensional array:
my2DArray[0][0] = 1; my2DArray[0][1] = 2; // Continue filling all elements -
Initializing a two-dimensional array with nested loops:
for (int i = 0; i < my2DArray.length; i++) { for (int j = 0; j < my2DArray[i].length; j++) { my2DArray[i][j] = (i * my2DArray.length) + j + 1; } } -
Creating and initializing a two-dimensional array (at declaration):
int[][] myInitialized2DArray = { {1, 2}, {3, 4}, {5, 6} };
Default Values of Arrays:
In Java, once an array is declared and initialized, all its elements are automatcially filled with the default value for that type. For primitive data types, integers default to 0, floating-point numbers to 0.0, booleans to false, and characters to \u0000.
Anonymous Arrays:
Java also allows the creation of anonymous arrays, i.e., creating an array without declaring a variable:
new int[] {10, 20, 30, 40, 50};
Traversing Arrays
Traversing an array means accessing each element in order. In Java, loops are commonly used for traversal. Below are several methods:
1. Using a for loop:
int[] myArray = {10, 20, 30, 40, 50};
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
2. Using a for-each loop (enhanced for loop):
for (int element : myArray) {
System.out.println(element);
}
3. Using Java 8 Stream API:
import java.util.Arrays;
public class ArrayTraversal {
public static void main(String[] args) {
int[] myArray = {10, 20, 30, 40, 50};
Arrays.stream(myArray).forEach(System.out::println);
}
}
4. Traditional for loop (reverse traversal):
for (int i = myArray.length - 1; i >= 0; i--) {
System.out.println(myArray[i]);
}
5. Random access (if specific order or condition is needed):
int randomIndex = ...; // Some way to obtain a random index
System.out.println(myArray[randomIndex]);
Notes:
- The
lengthproperty: Retrieves the length of the array, needed during traversal to know how many elements exist. - The
for-eachloop: Provides a concise way to traverse arrays and collections, hiding the index and making code more readable. - Index range: Ensure correct index range in
forloops to avoidArrayIndexOutOfBoundsException. - Readability: Choose the traversal method that fits your needs;
for-eachis typical more concise and readable.