Array
About
An array is a container object that holds a fixed number of elements of a single data type. Each element can be accessed by its index.
Fixed Size: The size of the array is defined when it is created and cannot be changed later.
Indexed Access: Elements are accessed using zero-based indexing.
Homogeneous Data: Arrays can only store elements of the same data type.
Memory Representation
1D Array: Contiguous block of memory.
2D Array: Memory is allocated row by row, with each row being an array itself.
Arrays are Objects: Arrays are treated as objects and inherit from
java.lang.Object
. We can call methods likegetClass()
on an array:
Default Values in Arrays: When an array is initialized, its elements are assigned default values:
0
for numeric types (int
,float
, etc.).false
forboolean
.null
for reference types (String
, objects).
Multidimensional Arrays: Java supports arrays of arrays. Example:
Exception
ArrayIndexOutOfBoundsException: Occurs when accessing an index outside the valid range.
NullPointerException: Accessing an uninitialized array.
Performance Characteristics: Arrays in Java are faster than many collection types like
ArrayList
due to:Contiguous Memory Allocation: Accessing elements via index is O(1).
No Boxing/Unboxing for Primitive Types: Arrays of primitives avoid the overhead of wrapping values in objects.
Declaration and Initialization of Arrays
Declaration
Initialization
1. Separate Declaration and Initialization:
2. Combined Declaration and Initialization:
3. Array Literals:
Accessing and Modification of Array Elements
Accessing
Modifying
Iterating Through an Array
For Loop:
Enhanced For Loop:
Properties and Methods of Arrays
Array Properties
length
length
Represents the number of elements in the array.
Syntax:
Array Methods
Java's built-in arrays do not have methods like objects of classes. However, the java.util.Arrays
class provides many static methods to work with arrays.
Methods in java.util.Arrays
java.util.Arrays
Method
Description
Example
copyOf
Copies the specified array, truncating or padding it to the specified length.
int[] newArray = Arrays.copyOf(original, newLength);
copyOfRange
Copies a range of elements from the original array.
int[] rangeArray = Arrays.copyOfRange(original, start, end);
sort
Sorts the elements of the array in ascending order.
Arrays.sort(array);
binarySearch
Searches for a specified value in a sorted array using the binary search algorithm.
int index = Arrays.binarySearch(array, value);
equals
Compares two arrays for equality (both length and content).
boolean isEqual = Arrays.equals(array1, array2);
deepEquals
Compares two multidimensional arrays for deep equality.
boolean isDeepEqual = Arrays.deepEquals(array1, array2);
toString
Returns a string representation of the array.
System.out.println(Arrays.toString(array));
deepToString
Returns a string representation of a multidimensional array.
System.out.println(Arrays.deepToString(array));
fill
Fills the array with a specified value.
Arrays.fill(array, value);
setAll
Sets all elements in the array based on a generator function.
Arrays.setAll(array, i -> i * 2);
asList
Converts an array to a List
.
List<Integer> list = Arrays.asList(array);
hashCode
Returns the hash code of the array.
int hash = Arrays.hashCode(array);
deepHashCode
Returns the hash code of a multidimensional array.
int hash = Arrays.deepHashCode(array);
stream
Converts the array into a Stream
for functional programming.
IntStream stream = Arrays.stream(array);
parallelSort
Sorts the array using parallel sorting algorithms for large datasets.
Arrays.parallelSort(array);
spliterator
Returns a Spliterator
for the array, which can be used for parallel processing.
Spliterator<Integer> split = Arrays.spliterator(array);
Types of Arrays
1D Array
A single row of elements.
2D Array
An array of arrays, useful for matrices
Jagged Array
An array with rows of varying lengths.
Array is Not thread-safe
Arrays in Java do not enforce synchronization for read or write operations. If two or more threads modify an array concurrently, the behavior depends on the timing of these modifications and may cause data corruption.
Making Arrays Thread-Safe
Use Synchronization
Wrap array operations in synchronized blocks or methods.
Use java.util.concurrent
Utilities
java.util.concurrent
UtilitiesUse thread-safe alternatives like CopyOnWriteArrayList
.
Use Atomic Variables
For arrays of integers or longs, use AtomicIntegerArray
or AtomicLongArray
Immutability
Make arrays immutable by copying them and sharing only the copy.
Arrays with Generics
Java arrays and generics don’t integrate quite well because arrays are covariant and generics are invariant. This mismatch introduces potential type-safety issues.
What Is Allowed
Creating Generic Arrays with a Wildcard
We can create arrays of wildcards (?
).
Creating Generic Arrays with Raw Types
We can create arrays of raw types (though it is not recommended due to loss of type safety).
Using Reflection to Create Generic Arrays
Reflection can bypass generic array restrictions.
Generic Arrays as Fields
We can declare a generic array field, but the actual instantiation must avoid direct generic array creation.
Using Arrays as Generic Collections
Although we cannot create a generic array directly, we can use collections like List
or ArrayList
.
What Is Not Allowed
Direct Creation of Generic Arrays
We cannot directly create arrays with a generic type.
Generic Arrays of Specific Parameterized Types
You cannot create an array of a specific generic type (e.g., List<String>
).
Mixing Arrays and Generics
You cannot mix arrays and generic types in assignments without warnings.
Example
Arrays can be created for almost all primitive and reference data types.
Primitive Data Types
byte
8-bit integer
byte[] byteArray = new byte[5];
short
16-bit integer
short[] shortArray = new short[5];
int
32-bit integer
int[] intArray = {1, 2, 3};
long
64-bit integer
long[] longArray = new long[5];
float
32-bit floating-point number
float[] floatArray = new float[5];
double
64-bit floating-point number
double[] doubleArray = new double[5];
char
16-bit Unicode character
char[] charArray = {'A', 'B'};
boolean
1-bit, can store true
or false
values
boolean[] boolArray = new boolean[5];
Reference Data Types
String
Stores sequences of characters
String[] strArray = {"A", "B", "C"};
Object
Parent class of all Java classes
Object[] objArray = new Object[5];
Custom Class
Any user-defined class can be used as an array type
Student[] studentArray = new Student[5];
Generic Types
List<?>
Array of wildcard lists (requires unchecked cast)
List<?>[] listArray = new List<?>[5];
Map<?, ?>
Array of maps
Map<String, Integer>[] mapArray = new Map[5];
Arrays Conversion to Streams
Single-Dimensional Arrays
Java provides Arrays.stream
method and the Stream.of
method to convert a single-dimensional array to a stream:
Using
Arrays.stream
:Using
Stream.of
:
Examples
Multi-Dimensional Arrays
Directly converting multi-dimensional arrays into streams isn't supported. However, we can flatten them into a single-dimensional structure or use nested streams.
Examples
Best Practices
Always check the array length before accessing elements.
Use enhanced for-loops for readability when traversing arrays.
Prefer
Arrays
utility methods for common operations like copying, filling, or sorting.
Last updated
Was this helpful?