← Back to Home

One-Dimensional Arrays

A one-dimensional array (1D array) in Java is a linear data structure used to store multiple values of the same data type in a contiguous memory sequence, accessed using an index. This is a foundational Core Java topic and appears frequently in interviews, algorithms, and real-time applications.

What Is a One-Dimensional Array?

  • Stores multiple values of the same type
  • Uses a single index for access
  • Index starts from 0
  • Size is fixed once created

Why Use Arrays?

  • Avoids creating multiple variables
  • Efficient data storage
  • Enables iteration and bulk processing
  • Base for collections and algorithms

Declaration of 1D Array

Syntax

dataType[] arrayName;
or
dataType arrayName[];
          

Preferred (industry standard):

int[] numbers;
          

Array Creation (Instantiation)

int[] numbers = new int[5];
          
  • Allocates memory for 5 elements
  • Default values are assigned automatically

Default Values in Arrays

Data Type Default Value
int 0
double 0.0
boolean false
char '\u0000'
Object null

Array Initialization

1. Static Initialization

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

2. Dynamic Initialization

int[] numbers = new int[4];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
          

Accessing Array Elements

int x = numbers[2];  // 30
          

⚠️ Accessing invalid index causes:

ArrayIndexOutOfBoundsException
          

Length of Array

int size = numbers.length;
          

Important: length is a property, not a method.

Traversing a 1D Array

Using for Loop

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

Using Enhanced for-each Loop

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

Example Program

int[] marks = {85, 90, 78};
int total = 0;
for (int m : marks) {
    total += m;
}
System.out.println("Total: " + total);
          

Common Operations on 1D Arrays

Find Maximum Element

int max = arr[0];
for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
        max = arr[i];
    }
}
          

Search an Element

int key = 20;
boolean found = false;
for (int n : arr) {
    if (n == key) {
        found = true;
        break;
    }
}
          

Memory Representation (Conceptual)

  • Array stores elements contiguously
  • Single reference points to the array
  • All elements are of same data type

Limitations of 1D Arrays

  • Fixed size
  • Cannot grow or shrink dynamically
  • Stores only same-type elements

Alternative: Use ArrayList for dynamic sizing.

Common Beginner Mistakes

  • Using invalid index
  • Confusing length with length()
  • Forgetting array initialization
  • Off-by-one errors in loops
  • Expecting dynamic resizing

Interview-Ready Answers

Short Answer

A one-dimensional array stores multiple values of the same type using a single index.

Detailed Answer

In Java, a one-dimensional array is a linear data structure that holds fixed-size elements of the same data type. It allows indexed access starting from zero and supports traversal using loops.

Key Takeaway

One-dimensional arrays are simple, fast, and memory-efficient, making them ideal for fixed-size sequential data. Mastering them is essential before moving to 2D arrays and collections.