Array in Java
An array in Java is a data structure used to store multiple values of the same
data type in a single variable. The values are stored in contiguous memory
locations and are accessed using an index.
Key Features of Arrays
Stores fixed-size elements
All elements must be of the same data type
Index starts from 0
Allows random access to elements
Declaring an Array
dataType[] arrayName;
or
dataType arrayName[];
Example:
int[] marks;
Creating an Array
marks = new int[5];
This creates an array that can store 5 integers.
Declaring and Initializing an Array in Java
Method 1: Declaration and Initialization in Separate Steps
Method 2: Declaration and Initialization in a Single Line
Java Program: Sum and Average of 5 Numbers Using Array
Java Program: Sum and Average of n Numbers Using Array
Two-Dimensional Array in Java
A two-dimensional (2D) array in Java is an array of arrays. It is used to store data
in rows and columns, similar to a table or matrix.
Java Program: Matrix Addition (3 × 3)