Slide 1
Slide 2
Slide 3

Access array elements in java programming (V44)

No Comments

Creating and Initializing Arrays
Method 1: Declare + Allocate Memory

Method 2: Declare + Initialize with Values
 

Accessing Elements 

Array elements are accessed using their index, starting from 0 for the first element.
Declaring and Processing Array Elements:

Source Code:

Copied!
public class AccesArray {
    public static void main(String[] args) {
        // Declare and initialize an array
        int[] numbers = {10, 20, 30, 40, 50};

        // Access and print specific elements
        System.out.println("First element: " + numbers[0]);   // Output: 10
        System.out.println("Third element: " + numbers[2]);   // Output: 30
        System.out.println("Last element: " + numbers[4]);    // Output: 50

        // Modify an element
        numbers[1] = 99;
        System.out.println("Updated second element: " + numbers[1]); // Output: 99
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top