One-Dimensional Array

Creating Arrays

Creating and Initializing One-Dimensional Arrays:

  1. Declaring an array:

    int[] myIntArray; // Declares an integer array
    
  2. Allocating memory (initializing the array):

    myIntArray = new int[5]; // Allocates an array that can store 5 integers
    
  3. Assigning array elements:

    myIntArray[0] = 10;
    myIntArray[1] = 20;
    myIntArray[2] = 30;
    myIntArray[3] = 40;
    myIntArray[4] = 50;
    
  4. Initializing an array with a loop:

    for (int i = 0; i < myIntArray.length; i++) {
        myIntArray[i] = i * 10; // Initializes each element to i * 10
    }
    
  5. Creating and initializing an array (at declaration):

    int[] myInitializedArray = {10, 20, 30, 40, 50};
    

Creating and Initializing Multi-Dimensional Arrays:

  1. Declaring a two-dimensional array:

    int[][] my2DArray;
    
  2. Allocating memory:

    my2DArray = new int[3][2]; // Allocates a 3x2 two-dimensional array
    
  3. Assigning elements to a two-dimensional array:

    my2DArray[0][0] = 1;
    my2DArray[0][1] = 2;
    // Continue filling all elements
    
  4. 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;
        }
    }
    
  5. 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 length property: Retrieves the length of the array, needed during traversal to know how many elements exist.
  • The for-each loop: Provides a concise way to traverse arrays and collections, hiding the index and making code more readable.
  • Index range: Ensure correct index range in for loops to avoid ArrayIndexOutOfBoundsException.
  • Readability: Choose the traversal method that fits your needs; for-each is typical more concise and readable.

Tags: array java programming one-dimensional array

Posted on Fri, 08 May 2026 02:42:51 +0000 by tqla