Arrays in Java are objects that store multiple variables of the same type. They provide a convenient way to handle large amounts of data of a similar nature. This guide will cover the basics of arrays, as well as more advanced concepts and operations.
A one-dimensional array is like a list of variables. The syntax for declaring an array is:
int[] arrayName; // Declares an array of integersTo initialize the array, you can use the new keyword:
arrayName = new int[10]; // Initializes the array with a size of 10Alternatively, you can declare and initialize the array in one step:
int[] arrayName = new int[10];
int[] numbers = {90, 85, 88, 92, 78}; // Declaration and initializationElements in an array are accessed using their index, with the first element having an index of 0:
int firstElement = numbers[0]; // Accesses the first element of the array
numbers[1] = 86; // Modifies the second element of the arrayThe length of an array is accessed using the length property:
int arrayLength = numbers.length; // Returns the length of the arrayYou can use a for loop to iterate over the elements of an array:
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}Using System.out.println to print an array directly will output a hash code. To print the contents, use Arrays.toString():
import java.util.Arrays;
System.out.println(Arrays.toString(numbers)); // Output: [90, 85, 88, 92, 78]Two-dimensional arrays are like tables with rows and columns. They are arrays of arrays.
int[][] arrayName = new int[3][4]; // 3 rows and 4 columnsAlternatively, you can initialize it directly:
int[][] marks = {
{90, 85, 88, 92},
{78, 88, 85, 90}
};Access elements using two indices: one for the row and one for the column:
int firstElement = marks[0][0]; // Accesses the first element in the first row
marks[1][2] = 95; // Modifies the third element in the second rowUse nested for loops to iterate through elements:
for (int i = 0; i < marks.length; i++) {
for (int j = 0; j < marks[i].length; j++) {
System.out.print(marks[i][j] + " ");
}
System.out.println();
}To print the entire 2D array, use Arrays.deepToString():
System.out.println(Arrays.deepToString(marks)); // Output: [[90, 85, 88, 92], [78, 88, 85, 90]]Use Arrays.sort() to sort an array in ascending order:
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // Output: [78, 85, 88, 90, 92]Use Arrays.copyOf() to copy an array:
int[] copiedArray = Arrays.copyOf(numbers, numbers.length);
System.out.println(Arrays.toString(copiedArray));Use Arrays.fill() to fill an array with a specific value:
int[] filledArray = new int[5];
Arrays.fill(filledArray, 1);
System.out.println(Arrays.toString(filledArray)); // Output: [1, 1, 1, 1, 1]Use Arrays.binarySearch() to search for a value in a sorted array:
int index = Arrays.binarySearch(numbers, 88);
System.out.println("Index of 88: " + index); // Output: Index of 88: 2Java supports arrays of more than two dimensions. The concept is similar to 2D arrays but with more indices:
int[][][] threeDArray = new int[3][4][2]; // 3x4x2 arrayAccess elements similarly by adding more indices:
threeDArray[0][1][1] = 5;Uninitialized array elements have default values based on their type:
- int, byte, short, long: 0
- float, double: 0.0
- char: '\u0000'
- boolean: false
- Objects: null
Java allows jagged arrays (arrays of arrays with different lengths):
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2];
jaggedArray[1] = new int[3];
jaggedArray[2] = new int[1];While arrays have a fixed size, ArrayList from the java.util package provides a dynamic array that can grow as needed:
import java.util.ArrayList;
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(20);
System.out.println(arrayList); // Output: [10, 20]Generics allow you to create classes, interfaces, and methods with a placeholder for types:
import java.util.ArrayList;
ArrayList<String> stringList = new ArrayList<>();
stringList.add("Hello");
stringList.add("World");
System.out.println(stringList); // Output: [Hello, World]Arrays in Java provide a powerful and efficient way to handle multiple data elements. From basic one-dimensional arrays to more complex multi-dimensional arrays and dynamic ArrayList, understanding and utilizing arrays effectively is crucial for Java programming. This guide covers the essentials and advanced topics, making it a comprehensive reference for working with arrays in Java.