Skip to content
C

One-Dimensional Array

A one-dimensional array is the simplest form of array — a single, straight line of values, all stored under one variable name and accessed with a single index.


1. What is a One-Dimensional Array?

A one-dimensional array is the simplest form of array — a single, straight line of values, all stored under one variable name and accessed with a single index.

2. Why is it used?

It's useful whenever you need to store a simple list of related values, like a list of temperatures recorded over a week, or the ages of students in a class.

3. Real-Life Example

Think of a single row of lockers in a school corridor, numbered from 1 onward. Each locker holds one item, and you access an item by simply knowing its locker number — that's a one-dimensional array.

4. Syntax

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

5. Example Program

java
public class OneDArrayDemo { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40}; for (int i = 0; i < numbers.length; i++) { System.out.println("Element at index " + i + ": " + numbers[i]); } } }

Output:

Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40

6. Key Points to Remember

  • .length gives the number of elements in the array (not .length(), which is used for String).
  • A one-dimensional array is ideal for simple lists that don't need rows and columns.
  • You can declare and initialize values together using curly braces {}.