Definition:
An array in Java is a collection of elements, all of the same data type, stored in contiguous memory locations. Arrays are a fixed-size, ordered collection of elements that can be primitive data types (like int, char, float) or objects (like String). The size of the array is determined at the time of creation and cannot be changed later.
Contents
Example:
public class ArrayExample {
public static void main(String[] args) {
// 1. Declare and Initialize an Array
int[] numbers = {10, 20, 30, 40, 50};
// 2. Access and Print Array Elements
System.out.println("First Element: " + numbers[0]);
System.out.println("Last Element: " + numbers[numbers.length - 1]);
// 3. Iterate Over the Array Using a For Loop
System.out.println("Array Elements:");
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
// 4. Calculate Sum of Array Elements
int sum = 0;
for (int num : numbers) { // Enhanced for loop
sum += num;
}
System.out.println("Sum of Elements: " + sum);
// 5. Create and Initialize a String Array
String[] names = {"Alice", "Bob", "Charlie"};
System.out.println("Names:");
for (String name : names) {
System.out.println(name);
}
}
}
Over all Output:
First Element: 10
Last Element: 50
Array Elements:
10
20
30
40
50
Sum of Elements: 150
Names:
Alice
Bob
Charlie
Features of Arrays in Java:
- The size of the array is fixed at the time of creation and cannot be changed dynamically. If you need a dynamic array, you can use classes like ArrayList .
- Arrays use zero-based indexing, meaning the first element is at index 0, the second element is at index 1, and so on.
- All elements in the array must be of the same data type (either all int, all String, etc.).
- Arrays allow efficient access to elements using their index, which makes them faster for lookups and traversals.
- Elements of an array are stored in contiguous memory locations, making it easier to access them via their index and providing better performance.
- Java supports both one-dimensional and multidimensional arrays (like 2D arrays or arrays of arrays).
- When an array is created, elements are automatically initialized with default values based on their data type (e.g., 0 for int, null for objects, false for boolean).
Advantages of Arrays in Java:
- Arrays allow storing multiple values of the same type in a compact format.
- Arrays provide fast random access to elements using their index. Accessing an element at any position has a time complexity of O(1).
- Arrays are stored in contiguous memory blocks, making them more efficient in terms of memory usage.
- Arrays make looping through data easy and efficient, as you can use for loops or enhanced for loops (for-each).
- Arrays form the basis of more advanced data structures such as lists , stacks, and queues.
Disadvantages:
- Once an array is created, its size cannot be changed. This can lead to wasted memory if the array size is too large or inefficient memory usage if it’s too small.
- Arrays lack advanced methods for data manipulation (like adding, removing, or sorting elements) that are available in more flexible collections like ArrayList.
Uses of Arrays in Java:
- Arrays are useful when you know the exact number of elements that need to be stored.
- Arrays are used to represent data in a tabular format, such as matrices or tables.
- Arrays are commonly used as buffers in programs where data needs to be temporarily stored before further processing.
- Arrays are used to handle a large number of objects, such as storing multiple Student or Employee objects in an application.
- Arrays are often used in algorithms for searching (e.g., binary search) or sorting (e.g., bubble sort, quicksort) because of their efficient access to elements.