Arrays in Java:
1. What is an Array in Java?
An array is a collection of similar data types stored in contiguous memory locations. Arrays are used
to store multiple values in a single variable instead of declaring separate variables for each value.
2. Use of Arrays in Java
Storing multiple values of the same data type together.
Accessing elements using an index number.
Useful in sorting and searching algorithms.
Helps in reducing code redundancy.
Used in matrices, graphs, and dynamic programming.
3. Syntax of Array in Java
Declaration and Initialization of an Array
dataType[] arrayName; // Declaring an array
arrayName = new dataType[size]; // Allocating memory
or
dataType[] arrayName = new dataType[size]; // Declaring and Allocating memory in one step
or
dataType[] arrayName = {value1, value2, value3, ...}; // Direct initialization
4. Types of Arrays in Java
4.1 One-Dimensional (1D) Array
A one-dimensional array stores elements in a single row.
Syntax
dataType[] arrayName = new dataType[size];
Use Case
Used in simple data storage, lists, queues, and searching algorithms.
Example: 1D Array
public class OneDArrayExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = {10, 20, 30, 40, 50};
// Access and print array elements
System.out.println("Array Elements:");
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + " : " + numbers[i]);
Output
Array Elements:
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
4.2 Two-Dimensional (2D) Array
A two-dimensional array is an array of arrays, represented in a matrix form.
Syntax
dataType[][] arrayName = new dataType[rows][columns];
or
dataType[][] arrayName = { {value1, value2}, {value3, value4} };
Use Case
Used in matrix operations, game development (grid-based games like Chess, Tic-Tac-Toe), and storing
tabular data.
Example: 2D Array (Matrix)
public class TwoDArrayExample {
public static void main(String[] args) {
// Declare and initialize a 2D array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Print the 2D array
System.out.println("2D Array (Matrix):");
for (int i = 0; i < matrix.length; i++) { // Row loop
for (int j = 0; j < matrix[i].length; j++) { // Column loop
System.out.print(matrix[i][j] + " ");
System.out.println(); // Move to the next row
Output
2D Array (Matrix):
123
456
789
4.3 Jagged Arrays
A jagged array is an array where each row can have a different number of columns.
Syntax
dataType[][] arrayName = new dataType[rows][];
Each row is initialized separately.
Use Case
Used when memory optimization is needed, such as in irregular data structures like triangle
matrices.
Example: Jagged Array
public class JaggedArrayExample {
public static void main(String[] args) {
// Declare a jagged array with different column sizes
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[]{1, 2}; // First row has 2 elements
jaggedArray[1] = new int[]{3, 4, 5}; // Second row has 3 elements
jaggedArray[2] = new int[]{6, 7, 8, 9}; // Third row has 4 elements
// Print the jagged array
System.out.println("Jagged Array:");
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
System.out.println();
Output
Jagged Array:
12
345
6789