Arrays
Introduction
An array in Java is a collection of elements of the same data type stored in a single variable. Each element in the array is accessed using an index number. Arrays are used to store multiple values in one variable instead of creating separate variables for each value.
Properties of Array
- Fixed Size
Once an array is created, its size cannot be changed. - Same Data Type
All elements in an array must be of the same data type (e.g., int, String). - Index Based
Each element is accessed using an index number starting from 0. - Contiguous Memory Allocation
Array elements are stored in continuous memory locations. - Object in Java
Arrays are treated as objects in Java. - Random Access
Any element can be accessed directly using its index.
Example
public class Main {
public static void main(String[] args) {
// declaration and initialization of array
int numbers[] = {10, 20, 30, 40, 50};
// accessing array elements
System.out.println("First Element: " + numbers[0]);
System.out.println("Second Element: " + numbers[1]);
System.out.println("Third Element: " + numbers[2]);
// printing all elements using loop
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
Advantages of Array
- Easy Access of Data
You can access any element quickly using index numbers. - Code Optimization
Reduces the need to declare multiple variables. - Easy to Use Loops
Arrays work well with loops for processing multiple values. - Better Memory Management
Stores multiple values in a single variable efficiently. - Fast Performance
Direct indexing makes data access very fast. - Supports Sorting and Searching
Arrays are widely used in sorting and searching algorithms.
Multi-Dimensional Array in Java
A multi-dimensional array in Java is an array that contains other arrays. It is mainly used to store data in the form of rows and columns, just like a table or matrix. It is also called an array of arrays.
- A multi-dimensional array is an array of arrays
- It stores data in rows and columns format
- It uses two indexes to access elements
- Indexing always starts from 0
Example
public class Main {
public static void main(String[] args) {
int[][] numbers = {
{1, 2, 3},
{4, 5, 6}
};
// Accessing elements
System.out.println(numbers[0][0]); // 1
System.out.println(numbers[0][2]); // 3
System.out.println(numbers[1][1]); // 5
System.out.println(numbers[1][2]); // 6
}
}
Disadvantages of Array
- Fixed Size
The size of an array is fixed. Once created, it cannot be increased or decreased. - Memory Wastage
If the array size is larger than needed, unused memory gets wasted. - Same Data Type Only
Arrays can store only one type of data (e.g., all int or all String). - Insertion and Deletion is Difficult
Adding or removing elements in an array is not easy and takes more time. - No Built-in Features
Arrays do not provide advanced features like searching, sorting, or dynamic resizing. - Risk of Index Errors
If you access an invalid index, it causes an error (ArrayIndexOutOfBoundsException).