Array in Java — Complete Guide
Array
Array is fixed size, index based linear data structure that stores multiple elements of the same type in single container inside a contiguous memory location. Array can be one dimensional or multidimensional.
Array provides efficient data management, memory optimization and cleaner code. Array help to handle large sets of related data by reducing complexity of code.
Array offers fixed size, easy access, performance critical applications and memory and performance efficiency.
Array indices start at 0 because an index represents how far an element is from the start of memory — and the first element is 0 steps away.
Key Characteristics
- Fixed size (cannot grow or shrink)
- Stores elements of the same type
- Indexed (0-based index)
- Can store primitives or objects
You can access each item using single index and that indices start from 0.
If you define multiple variable instead of array that can lead to following issue
- Repetitive Code
- Updating variable multiple times
- Possible Scalability issues
- Reduced code readability
- increased chance of errors
1D Array
A one-dimensional (1D) array is a list of elements of the same data type arranged in a single row. It is accessed using one index.
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[2]); // 302D Array
A two-dimensional (2D) array is an array of arrays, arranged in rows and columns (like a table or matrix).
It is accessed using two indices: row and column.
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(matrix[1][2]); // 63D Array
A three-dimensional (3D) array is an array of 2D arrays, forming a cube-like structure.
It is accessed using three indices: layer, row, and columnArray:
int[][][] cube = {
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
}
};
System.out.println(cube[1][0][1]); // 6Declaring Array
// approach 1 -> when we know all the elements need to declare in advance
String [] daysOfWeek1 =
{"MONDAY", "TUESDAY", "WEDNESDAY","THURSDAY",
"FRIDAY", "SATURDAY", "SUNDAY"};
/* approach 2 ->
when th element of array not known at the time of declaration or
need to be computed or retrieved from other source
add days dynamically intialization
*/
String [] daysOfWeek2 = new String[7];
daysOfWeek2[0] = "Sunday";
daysOfWeek2[1] = "Monday";
daysOfWeek2[2] = "Tuesday";
daysOfWeek2[3] = "Wednesday";
daysOfWeek2[4] = "Thursday";
daysOfWeek2[5] = "Friday";
daysOfWeek2[6] = "Saturday";
/* approach 3 -> populating dynamically using other source */
String [] daysOfWeek3 = new String[7];
String [] DAYS =
{"MONDAY", "TUESDAY", "WEDNESDAY","THURSDAY",
"FRIDAY", "SATURDAY", "SUNDAY"};
for(int day = 0; day < DAYS.length; day++) {
daysOfWeek3[index] = day[index];
}Accessing Element
Access element in the array using the position index.
// 1D Array Access
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[0]); // 10
System.out.println(numbers[2]); // 30
// 2D Array Access
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(matrix[0][1]); // 2
System.out.println(matrix[1][2]); // 6
// 3D Array Access
int[][][] cube = {
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
}
};
System.out.println(cube[1][0][1]); // 6Traversal →
Traversal is the process of accessing each element of an array one by one.
Get Kaizen Chandra’s stories in your inbox
Join Medium for free to get updates from this writer.
In a calendar application, traversal allows users to browse days, weeks, or months to see scheduled events.
Insertion → process of adding a new element to a specific index
- add without exceeding the array capacity
- ensure usability within a predefined size
Deletion →
Deletion means removing an element from the array.
Because arrays are fixed-size, deletion is handled in two common ways:
- Replace the element with null
- Create a new array without the deleted element
Insertion →
Insertion Without Changing Array Size (Replacement)
This approach is used when:
- The array has a fixed capacity
- The index already exists
- You simply want to modify an element
Insertion by Increasing Array Size
This approach is used when:
- The array is full
- You need to add more elements
- Flexibility is required
Inserting or deleting elements in the middle of an array requires extra memory
Deleting and replacing element with null value can waste memory because the array size remains unchanged
Array Length Property
Arrays have a built-in length property (not a method).
int[] arr = {1, 2, 3, 4};
System.out.println(arr.length); // 4Jagged Arrays (Uneven Arrays)
Arrays where inner arrays have different lengths.
int[][] jagged = new int[3][];
jagged[0] = new int[]{1, 2};
jagged[1] = new int[]{3, 4, 5};
jagged[2] = new int[]{6};Arrays of Objects
Arrays can store object reference
record Student(String name) {}
Student[] students = new Student[2];
students[0] = new Student("Alice");
students[1] = new Student("Bob");Array Operation
// copy operation
int[] src = {2, 3, 1};
int[] dest = new int[3];
System.arraycopy(src, 0, dest, 0, src.length);
// sorting
Arrays.sort(src);
// searching
int index = Arrays.binarySearch(src,3);
// comparing
boolean isEquals = Arrays.equals(src,new int [] {1,2,3});
// fill
Arrays.fill(dst, -1);
// copyOf
Arrays.copyOf(src, 2)
// copyOfRange
Arrays.copyOfRange(src, 0, 1);
int mismatchFirstIndex = Arrays.mismatch(src, dest);
// Finds and returns the index of the first mismatch between two int arrays, otherwise return -1 if no mismatch is found.
Random random = new SecureRandom();
List<Integer> list = IntStream.of(1, 2, 3, 4 )
.boxed().toList();
Integer[] array1 = IntStream.range(1, 10).boxed().toArray(Integer[]::new);
Integer[] array2 = IntStream.rangeClosed(1,10).boxed().toArray(Integer[]::new);
int[] array3 = IntStream.iterate(1, n -> n + 1).limit(10).toArray();
int[] array4 = IntStream.generate(random::nextInt).limit(10).toArray();
int[] array5 = IntStream.builder().add(1).add(2).add(3).add(4).add(5).build().toArray();
Spliterator<Integer> spliterator = Arrays.spliterator(src);
// Process one element
spliterator.tryAdvance(System.out::println); // 1
// Process remaining elements // 2,3
spliterator.forEachRemaining(System.out::println);
int [] arr = new int [] {10, 20, 30, 40, 50};
Spliterator<Integer> spliterator1 = Arrays.spliterator(arr);
Spliterator<Integer> spliterator2 = spliterator1.trySplit();
System.out.println("Spliterator 1:");
spliterator1.forEachRemaining(System.out::println);
System.out.println("Spliterator 2:");
spliterator2.forEachRemaining(System.out::println);









