One-dimensional Arrays in Java
An array is a container object that holds a fixed number of values of a single type. In Java, an array is an indexed collection of elements of the same type. A one-dimensional array is essentially a list of values that can be accessed using an index.
1. Declaring a One-dimensional Array
To declare a one-dimensional array, we specify the type of the elements and use square brackets to indicate that it's an array. Here's the syntax:
type[] arrayName;
For example, if you want to create an array of integers:
int[] numbers;
Example 1: Declaring an Integer Array
In this example, we declare a one-dimensional array of integers:
public class Main { public static void main(String[] args) { // Declare an array of integers int[] numbers; // Initialize the array with 5 elements numbers = new int[5]; // Assign values to the array numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; numbers[3] = 40; numbers[4] = 50; // Print the values in the array for (int i = 0; i < numbers.length; i++) { System.out.println("Element at index " + i + ": " + numbers[i]); } } }
Output:
Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40 Element at index 4: 50
2. Initializing a One-dimensional Array
Arrays can also be initialized at the time of declaration. Here's the syntax for array initialization:
type[] arrayName = {value1, value2, value3, ...};
Example 2: Initializing an Array
In this example, we initialize the array with values at the time of declaration:
public class Main { public static void main(String[] args) { // Declare and initialize the array in one step int[] numbers = {10, 20, 30, 40, 50}; // Print the values in the array for (int i = 0; i < numbers.length; i++) { System.out.println("Element at index " + i + ": " + numbers[i]); } } }
Output:
Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40 Element at index 4: 50
3. Accessing Array Elements
Array elements are accessed using their index, starting from 0. The index of the first element is 0, the second element is 1, and so on.
Example 3: Accessing Array Elements
In this example, we access specific elements of the array using their index:
public class Main { public static void main(String[] args) { // Declare and initialize the array int[] numbers = {10, 20, 30, 40, 50}; // Access elements by index System.out.println("Element at index 0: " + numbers[0]); System.out.println("Element at index 2: " + numbers[2]); System.out.println("Element at index 4: " + numbers[4]); } }
Output:
Element at index 0: 10 Element at index 2: 30 Element at index 4: 50
4. Array Length
The length of an array is the number of elements it can hold. You can access the length of an array using the length
property:
arrayName.length;
Example 4: Getting the Length of an Array
In this example, we use the length
property to get the number of elements in the array:
public class Main { public static void main(String[] args) { // Declare and initialize the array int[] numbers = {10, 20, 30, 40, 50}; // Get the length of the array System.out.println("Array length: " + numbers.length); } }
Output:
Array length: 5
5. Iterating Over an Array
You can use loops to iterate over the elements of an array. The most common loop used for this purpose is the for
loop, but you can also use the enhanced for
loop (also known as the "for-each" loop).
Example 5: Iterating Over an Array Using a Standard For Loop
In this example, we use a standard for
loop to iterate over the array:
public class Main { public static void main(String[] args) { // Declare and initialize the array int[] numbers = {10, 20, 30, 40, 50}; // Iterate over the array using a standard for loop for (int i = 0; i < numbers.length; i++) { System.out.println("Element at index " + i + ": " + numbers[i]); } } }
Output:
Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 Element at index 3: 40 Element at index 4: 50
Example 6: Iterating Over an Array Using the Enhanced For Loop
In this example, we use the enhanced for
loop to iterate over the array:
public class Main { public static void main(String[] args) { // Declare and initialize the array int[] numbers = {10, 20, 30, 40, 50}; // Iterate over the array using an enhanced for loop for (int num : numbers) { System.out.println("Element: " + num); } } }
Output:
Element: 10 Element: 20 Element: 30 Element: 40 Element: 50
6. Modifying Array Elements
You can modify the elements of an array by accessing them using their index and assigning a new value:
Example 7: Modifying Array Elements
In this example, we modify the values of specific elements in the array:
public class Main { public static void main(String[] args) { // Declare and initialize the array int[] numbers = {10, 20, 30, 40, 50}; // Modify the value at index 2 numbers[2] = 100; // Print the modified array for (int i = 0; i < numbers.length; i++) { System.out.println("Element at index " + i + ": " + numbers[i]); } } }
Output:
Element at index 0: 10 Element at index 1: 20 Element at index 2: 100 Element at index 3: 40 Element at index 4: 50
7. Conclusion
One-dimensional arrays in Java are simple and effective structures for storing multiple values of the same type. By understanding how to declare, initialize, access, and manipulate arrays, you can efficiently manage collections of data in your Java programs.