Single Dimensional Arrays

A single-dimensional array in Java is a linear data structure that holds a fixed-size sequence of elements of the same type. It is one of the most commonly used data structures in Java, particularly when you need to store and access a list of related data elements, such as numbers, strings, or objects.

Characteristics of Single-Dimensional Arrays:

  1. Fixed Size: Once an array is declared, its size cannot be changed.

  2. Index-Based Access: Elements in an array are accessed via their index, with the first element having an index of 0.

  3. Homogeneous Data: All elements in an array are of the same type (e.g., all integers, all strings, etc.).

Syntax for Declaring and Initializing a Single-Dimensional Array:

  1. Declaration:

type[] arrayName;Code language: CSS (css)
  • type: The data type of the elements (e.g., int, String, double).
  • arrayName: The name of the array variable.

Initialization:

  • You can initialize an array when you declare it or later in the program.

Example 1 (Declaration and Initialization at the Same Time):

int[] numbers = new int[5];  // Creates an array with 5 elements, initialized to 0
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;Code language: JavaScript (javascript)

Example 2 (Declaration First, then Initialization):

int[] numbers = new int[5];  // Creates an array with 5 elements, initialized to 0
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;Code language: JavaScript (javascript)

Accessing Elements in a Single-Dimensional Array:

  • Array elements are accessed using indices, which start from 0.

    Example:

System.out.println(numbers[0]);  // Output: 10
System.out.println(numbers[4]);  // Output: 50Code language: JavaScript (javascript)

Iterating Through a Single-Dimensional Array:

You can loop through an array using a for loop or an enhanced for loop (also known as the “for-each” loop).

Example using a for loop:

for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

Example using an enhanced for loop:

for (int number : numbers) {
    System.out.println(number);
}

Key Methods for Arrays:

  1. length: The length property gives the number of elements in the array.

System.out.println(numbers.length);  // Output: 5Code language: JavaScript (javascript)

2. Arrays.toString(): You can use the Arrays.toString() method (from java.util.Arrays) to print an array in a human-readable format.

System.out.println(Arrays.toString(numbers));  // Output: [10, 20, 30, 40, 50]Code language: JavaScript (javascript)

Example Program Using a Single-Dimensional Array:

import java.util.Arrays;

public class ArrayExample {
    public static void main(String[] args) {
        // Declare and initialize the array
        int[] numbers = {1, 2, 3, 4, 5};
        
        // Accessing elements in the array
        System.out.println("First element: " + numbers[0]);
        System.out.println("Last element: " + numbers[4]);
        
        // Iterating over the array using a for loop
        System.out.println("\nUsing for loop:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }

        // Iterating over the array using the enhanced for loop
        System.out.println("\nUsing enhanced for loop:");
        for (int num : numbers) {
            System.out.println(num);
        }

        // Printing the array using Arrays.toString()
        System.out.println("\nArray contents: " + Arrays.toString(numbers));
    }
}
/*
First element: 1
Last element: 5

Using for loop:
1
2
3
4
5

Using enhanced for loop:
1
2
3
4
5

Array contents: [1, 2, 3, 4, 5]
*/

A single-dimensional array is a simple yet powerful data structure for storing a sequence of elements in Java. Understanding how to declare, initialize, access, and manipulate arrays is essential for working with many Java programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top