← Back to Chapters

Single Dimensional Array in Java

? Single Dimensional Array in Java

? Quick Overview

A single dimensional array in Java is a collection of elements of the same data type stored in a continuous memory location. Each element is accessed using an index starting from 0.

? Key Concepts

  • Arrays store multiple values in a single variable
  • Index starts from 0 and ends at length - 1
  • Fixed size once declared
  • All elements are of the same data type

? Syntax / Theory

Java provides multiple ways to declare and initialize a single dimensional array.

? View Code Example
// Declaration of an integer array
int[] numbers;

// Allocation of memory for 5 integers
numbers = new int[5];

? Code Example

? View Code Example
// Program to demonstrate single dimensional array
public class Main {
public static void main(String[] args) {

// Creating and initializing the array
int[] marks = {85, 90, 78, 92, 88};

// Accessing array elements using loop
for(int i = 0; i < marks.length; i++) {
System.out.println("Marks: " + marks[i]);
}
}
}

? Live Output / Explanation

Output

Marks: 85
Marks: 90
Marks: 78
Marks: 92
Marks: 88

Each value is printed by accessing the array using its index inside a loop.

✅ Tips & Best Practices

  • Always use array.length instead of hardcoded values
  • Initialize arrays at the time of declaration when possible
  • Use meaningful variable names for clarity
  • Avoid accessing indexes outside array bounds

? Try It Yourself

  • Create an array of 10 numbers and print only even values
  • Find the sum of all elements in an array
  • Store student names in a String array and display them