Working with One-Dimensional Arrays in Java
An array in Java is a fixed-size container that holds elements of the same type. Once created, its length cannot be changed. Elements are accessed using an index starting from 0.
Declaring Array Variables
To use an array, first declare a reference variable specifying the element type:
double[] values; // Preferred style
// or
double values[]; // C-style (less common in Java)
This declaration does not allocate memory—it only creates a reference that initially holds null.
Creating and Initializing Arrays
Use the new operator to allocate memory for the array:
double[] values = new double[10];
Alternatively, initialize the array inline using curly braces:
int[] numbers = {10, 20, 30};
Note: The shorthand initialization must be done at the time of declaration. The following is invalid:
int[] arr;
arr = {1, 2, 3}; // Compilation error
Accessing Elements and Enhanced For Loop
Array indices range from 0 to length - 1. Elements can be read or modified like regular variables:
int[] data = new int[5];
data[0] = 42;
The enhanced for loop (foreach) simplifies iteration:
for (double item : values) {
System.out.println(item);
}
Copying Arrays
Assigning one array variable to another copies the reference, not the contents. To create a true copy:
-
Manual loop:
int[] src = {1, 2, 3}; int[] dest = new int[src.length]; for (int i = 0; i < src.length; i++) { dest[i] = src[i]; } -
System.arraycopy:System.arraycopy(src, 0, dest, 0, src.length);
This method requires the destination array to already exist.
Passing and Returning Arrays
Arrays are objects, so when passed to a method, the reference is passed by value. Modifications inside the method affect the original array:
public static void modify(int[] arr) {
arr[0] = 999; // affects the original array
}
Methods can also return arrays:
public static int[] generateSequence(int size) {
int[] seq = new int[size];
for (int i = 0; i < size; i++) {
seq[i] = i;
}
return seq; // returns reference to the new array
}
Varible-Length Argument Lists
A method can accept a variable number of arguments of the same type using ...:
public static void displayMax(double... values) {
if (values.length == 0) {
System.out.println("No values provided");
return;
}
double max = values[0];
for (int i = 1; i < values.length; i++) {
if (values[i] > max) max = values[i];
}
System.out.println("Maximum: " + max);
}
// Usage
displayMax(3.5, 7.2, 1.8); // Passes as double[] internally
Rules:
- Only one varargs parameter allowed.
- It must be the last parameter.
Searching Algorithms
Linear Search: Checks each element sequentially.
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) return i;
}
return -1;
}
Binary Search: Requires a sorted array.
public static int binarySearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (target < arr[mid]) {
high = mid - 1;
} else if (target == arr[mid]) {
return mid;
} else {
low = mid + 1;
}
}
return -1;
}
The Arrays Utility Class
The java.util.Arrays class provides helpful static methods:
-
Sorting:
double[] nums = {3.2, 1.5, 4.8}; Arrays.sort(nums); // ascending order Arrays.parallelSort(nums); // parallel version // Partial sort: indices [1, 3) char[] letters = {'z', 'b', 'a', 'c'}; Arrays.sort(letters, 1, 3); -
Binary Search (array must be sorted):
int[] sorted = {2, 4, 7, 10, 11}; int pos = Arrays.binarySearch(sorted, 7); // returns 2 int missing = Arrays.binarySearch(sorted, 5); // returns -(insertion point + 1) = -3 -
Equality Check:
int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; int[] c = {3, 2, 1}; System.out.println(Arrays.equals(a, b)); // true System.out.println(Arrays.equals(b, c)); // false -
String Representation:
System.out.println(Arrays.toString(a)); // prints [1, 2, 3] -
Filling Arrays:
int[] buffer = new int[5]; Arrays.fill(buffer, 7); // [7, 7, 7, 7, 7] int[] partial = {0, 0, 0, 0, 0}; Arrays.fill(partial, 1, 4, 9); // [0, 9, 9, 9, 0]