Introduction to Arrays

An array in Java is a data structure that stores multiple values of the same type in a single variable. It is a fixed-size, ordered collection where each element is accessible using an index.

  • Fixed Size: Once declared, the size of an array cannot be changed.
  • Indexed Access: Elements are accessed using zero-based indexing (array[0] is the first element).
  • Same Data Type: All elements must be of the same type (e.g., int, double, String, etc.).
  • Stored in Memory Contiguously: Arrays provide fast access to elements using indices.

Syntax

  • dataType → The type of values the array will store (e.g., int, double, String).
  • arrayName → The name of the array.
  • size → The number of elements the array can hold.

Declaring and Initializing an Array

Or, you can declare and initialize in a single step:

Store Values in an Array

Once an array is created, we store values using indexing.

  • Indexes start from 0, meaning the first element is at index 0, the second at 1, and so on.

Output

Access and Change Values in an Array

We access and modify array elements using their index positions.