Arrays are fundamental data structures in Java, allowing efficient storage and manipulation of multiple elements of the same type. This unit covers array creation, initialization, accessing elements, and common operations like searching and sorting. Understanding arrays is crucial for organizing and processing data in programming. The unit explores multidimensional arrays, iteration techniques, and common pitfalls to avoid when working with arrays in Java.
[]
and the array variable name
int[] numbers;
creates an array of integers named numbers
new
keyword followed by the data type and the desired length in square brackets
String[] names = new String[5];
creates an array of 5 String elements{}
double[] prices = {9.99, 14.50, 7.75};
creates an array with the specified values[]
after the array variable name
numbers[0]
accesses the first element of the numbers
arraylength - 1
for the last elementnames[2] = "John";
assigns the value "John" to the third element of the names
arrayArrayIndexOutOfBoundsException
length
property is used to obtain the length of an array
int size = numbers.length;
retrieves the length of the numbers
arraylength - 1
, where length - 1
represents the index of the last elementArrayIndexOutOfBoundsException
names[5]
in an array of length 5 will result in an exceptionlength
property and valid indices when working with arrays to avoid accessing elements outside the array bounds[]
int value = numbers[3];
retrieves the fourth element of the numbers
arrayprices[1] = 12.99;
updates the second element of the prices
arrayArrays.binarySearch(numbers, 10)
searches for the value 10 in the numbers
arrayArrays.sort(names);
sorts the elements of the names
array in ascending orderSystem.arraycopy()
method or by manually iterating and copying elements
System.arraycopy(source, 0, destination, 0, length);
copies elements from the source
array to the destination
arrayArrays.equals()
method, which checks if two arrays have the same elements in the same orderfor
loop
length - 1
for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }
for
loop (for-each loop) can also be used to iterate through an array without explicitly using an index
for (String name : names) { System.out.println(name); }
[][]
int[][] matrix = new int[3][4];
creates a 2D array with 3 rows and 4 columnsmatrix[1][2]
accesses the element at the second row and third column{}
int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
creates a 3x3 2D array with the specified valuesfor (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); }
double[] scores = new double[10];
creates an array of 10 double elementsscores[4] = 85.5;
assigns the value 85.5 to the fifth element of the scores
arrayArrays.sort(scores);
sorts the elements of the scores
array in ascending orderfor
loops and enhanced for
loops
for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); }
scores[10]
in an array of length 10 will throw an ArrayIndexOutOfBoundsException