0
0
Javaprogramming~15 mins

One-dimensional arrays in Java

Choose your learning style8 modes available
menu_bookIntroduction

One-dimensional arrays help you store many values of the same type in a single place. This makes it easy to organize and use related data together.

When you want to keep a list of student scores in a test.
When you need to store daily temperatures for a week.
When you want to hold a sequence of names or words.
When you want to process a collection of numbers for calculations.
When you want to keep track of inventory counts for products.
regular_expressionSyntax
Java
public class OneDimensionalArray {
    public static void main(String[] args) {
        // Declare and create an array of integers
        int[] numbers = new int[5];

        // Assign values to the array
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        numbers[3] = 40;
        numbers[4] = 50;

        // Access and print values
        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Element at index " + i + ": " + numbers[i]);
        }
    }
}

The array size is fixed once created and cannot be changed.

Array indexes start at 0, so the first element is at index 0.

emoji_objectsExamples
line_end_arrow_notchThis creates an empty array with no elements.
Java
int[] emptyArray = new int[0];
// An array with zero elements
line_end_arrow_notchThis array holds only one value at index 0.
Java
int[] singleElementArray = new int[1];
singleElementArray[0] = 100;
// Array with one element at index 0
line_end_arrow_notchYou can create and fill an array in one step using curly braces.
Java
int[] numbers = {5, 10, 15, 20};
// Array initialized with values directly
line_end_arrow_notchUse length - 1 to get the last index because indexes start at 0.
Java
int lastElement = numbers[numbers.length - 1];
// Access the last element of the array
code_blocksSample Program

This program creates an array of 5 ages, prints them, changes one age, and prints the array again to show the update.

Java
public class OneDimensionalArrayExample {
    public static void main(String[] args) {
        // Create an array to store 5 ages
        int[] ages = new int[5];

        // Assign ages
        ages[0] = 21;
        ages[1] = 25;
        ages[2] = 30;
        ages[3] = 22;
        ages[4] = 28;

        // Print all ages before change
        System.out.println("Ages before update:");
        for (int i = 0; i < ages.length; i++) {
            System.out.println("Index " + i + ": " + ages[i]);
        }

        // Update the age at index 2
        ages[2] = 35;

        // Print all ages after change
        System.out.println("\nAges after update:");
        for (int i = 0; i < ages.length; i++) {
            System.out.println("Index " + i + ": " + ages[i]);
        }
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Accessing an index outside the array size causes an ArrayIndexOutOfBoundsException.

line_end_arrow_notch

Arrays have a fixed size; to change size, create a new array and copy values.

line_end_arrow_notch

Use loops to process all elements easily.

list_alt_checkSummary

One-dimensional arrays store multiple values of the same type in order.

Indexes start at 0 and go up to length - 1.

Arrays have fixed size and are useful for grouping related data.