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

    1. 1D Array: Contiguous block of memory.

    2. 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 like getClass() on an array:

int[] numbers = {1, 2, 3};
System.out.println(numbers.getClass().getName());  // Output: [I
  • Default Values in Arrays: When an array is initialized, its elements are assigned default values:

    • 0 for numeric types (int, float, etc.).

    • false for boolean.

    • null for reference types (String, objects).

  • Multidimensional Arrays: Java supports arrays of arrays. Example:

int[][] matrix = new int[3][3];
matrix[0][0] = 1;
  • Exception

ArrayIndexOutOfBoundsException: Occurs when accessing an index outside the valid range.

NullPointerException: Accessing an uninitialized array.

Consider Memory Usage: Large arrays can lead to OutOfMemoryError. Monitor memory with tools like JVisualVM.

  • 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

Creating Arrays with Reflection

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

  • Represents the number of elements in the array.

  • Syntax:

It is not a method, so parentheses () are not used.

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

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

Use 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

  1. Creating Generic Arrays with a Wildcard

We can create arrays of wildcards (?).

  1. Creating Generic Arrays with Raw Types

We can create arrays of raw types (though it is not recommended due to loss of type safety).

  1. Using Reflection to Create Generic Arrays

Reflection can bypass generic array restrictions.

  1. Generic Arrays as Fields

We can declare a generic array field, but the actual instantiation must avoid direct generic array creation.

  1. 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

  1. Direct Creation of Generic Arrays

We cannot directly create arrays with a generic type.

  1. Generic Arrays of Specific Parameterized Types

You cannot create an array of a specific generic type (e.g., List<String>).

  1. Mixing Arrays and Generics

You cannot mix arrays and generic types in assignments without warnings.

Reason for Restrictions

  • Covariance of Arrays: Arrays in Java are covariant, meaning String[] is a subtype of Object[].

  • Invariance of Generics: Generics are invariant, so List<String> is not a subtype of List<Object>. This ensures type safety at compile time.

  • Type Erasure: Generic types are erased at runtime, leaving no information about the actual type parameter. This makes it impossible to enforce type safety for generic arrays.

Example

Arrays can be created for almost all primitive and reference data types.

Primitive Data Types

Data Type
Description
Example Declaration

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

Data Type
Description
Example Declaration

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

Data Type
Description
Example Declaration

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:

  1. Using Arrays.stream:

  2. 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

  1. Always check the array length before accessing elements.

  2. Use enhanced for-loops for readability when traversing arrays.

  3. Prefer Arrays utility methods for common operations like copying, filling, or sorting.

Last updated