|
| 1 | +# `Array` in Java |
| 2 | + |
| 3 | +## Overview |
| 4 | +An array in Java is a data structure that can hold multiple values of the same type. Arrays in Java are zero-indexed, meaning the first element is accessed at index `0`. Once the size of an array is defined, it cannot be changed. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## Characteristics of Arrays |
| 9 | + |
| 10 | +| **Feature** | **Details** | |
| 11 | +|----------------------|-------------------------------------------------------| |
| 12 | +| **Package** | `java.lang` | |
| 13 | +| **Fixed Size** | The size of an array is fixed once initialized. | |
| 14 | +| **Indexed** | Elements are accessed using an index. | |
| 15 | +| **Default Values** | Numeric types default to `0`, boolean defaults to `false`, and object references to `null`. | |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## Key Features of Arrays |
| 20 | + |
| 21 | +- **Fixed Size**: Once an array is initialized, its size cannot be changed. |
| 22 | +- **Zero-Indexing**: Arrays are zero-indexed, meaning the first element is accessed at index `0`. |
| 23 | +- **Homogeneous Elements**: All elements in an array are of the same data type. |
| 24 | +- **Memory Efficiency**: Arrays are efficient in terms of memory storage for large amounts of data. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Operations on Array Types |
| 29 | + |
| 30 | +### **Array Operations** |
| 31 | + |
| 32 | +| **Operation** | **Symbol** | **Example** | |
| 33 | +|-----------------------|---------------|--------------------------------------------------| |
| 34 | +| Accessing Elements | `[]` | `arr[0]` | |
| 35 | +| Length of Array | `.length` | `arr.length` | |
| 36 | +| Initializing Array | `new` | `type[] arr = new type[5];`<br> eg:- <br> `int`[] arr = new `int`[5];<br> `double`[] arr = new `double`[5]; <br> `String`[] arr = new `String`[5]; | |
| 37 | +| Iterating over Array | `for-each` | for (`int` num : arr) { <br> System.out.println(num); <br> } | |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +# Static Methods |
| 42 | + |
| 43 | +| **Method** | **Description** | **Return Type** | **Example** | |
| 44 | +|---------------------------------------------|----------------------------------------------------------------------------|--------------------|-------------------------------------------------| |
| 45 | +| `copyOf(int[] original, int newLength)` | Copies the specified array, truncating or padding with `0` or `null`. | `int[]` | `Arrays.copyOf(arr, 10);` | |
| 46 | +| `fill(int[] array, int val)` | Fills the array with the specified value. | `void` | `Arrays.fill(arr, 5);` | |
| 47 | +| `equals(int[] a, int[] b)` | Compares two arrays for equality. | `boolean` | `Arrays.equals(arr1, arr2);` | |
| 48 | +| `sort(int[] a)` | Sorts the specified array in ascending order. | `void` | `Arrays.sort(arr);` | |
| 49 | +| `binarySearch(int[] a, int key)` | Searches for a value in a sorted array. Returns the index of the search key, or a negative value if not found. | `int` | `Arrays.binarySearch(arr, 5);` | |
| 50 | +| `deepToString(Object[] arr)` | Returns a string representation of a multidimensional array. | `String` | `Arrays.deepToString(arr);` | |
| 51 | +| `toString(Object[] arr)` | Returns a string representation of the array, using `Arrays.toString()` to format the array as a string. | `String` | `Arrays.toString(arr);` | |
| 52 | +| `copyOfRange(T[] arr, int from, int to)` | Copies a specified range from the array to a new array. | `T[]` | `Arrays.copyOfRange(arr, 2, 5);` | |
| 53 | +| `compare(T[] arr1, T[] arr2)` | Compares two arrays lexicographically. Returns a negative value if the first array is less than the second, positive if greater, or 0 if equal. | `int` | `Arrays.compare(arr1, arr2);` | |
| 54 | +| `stream(T[] arr)` | Converts the array into a stream, enabling operations like filtering, mapping, and reducing. | `Stream<T>` | `Arrays.stream(arr1);` | `T Stream` | `Arrays.stream(arr1)` | |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## Instance Methods |
| 59 | + |
| 60 | +| **Method** | **Description** | **Return Type** | **Example** | |
| 61 | +|-------------|------------------------------------------|--------------------|----------------------------------------------| |
| 62 | +| `length` | Returns the length of the array. | `int` | `arr.length;` | |
| 63 | +| `clone()` | Creates a copy of the array. | `Object` | `arr.clone();` | |
| 64 | +| `hashCode()`| Returns a hash code value for the array. | `int` | `arr.hashCode();` | |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Example Code |
| 69 | + |
| 70 | +```java |
| 71 | +import java.util.Arrays; |
| 72 | + |
| 73 | +public class ArrayExample { |
| 74 | + public static void main(String[] args) { |
| 75 | + // Create an array |
| 76 | + int[] arr = {5, 2, 9, 1, 5, 6}; |
| 77 | + |
| 78 | + // Get the length of the array |
| 79 | + System.out.println("Array length: " + arr.length); |
| 80 | + |
| 81 | + // Access elements |
| 82 | + System.out.println("Element at index 0: " + arr[0]); |
| 83 | + |
| 84 | + // Sort the array |
| 85 | + Arrays.sort(arr); |
| 86 | + System.out.println("Sorted array: " + Arrays.toString(arr)); |
| 87 | + |
| 88 | + // Search for an element |
| 89 | + int index = Arrays.binarySearch(arr, 5); |
| 90 | + System.out.println("Index of 5: " + index); |
| 91 | + |
| 92 | + // Fill the array with a specific value |
| 93 | + Arrays.fill(arr, 0); |
| 94 | + System.out.println("Filled array: " + Arrays.toString(arr)); |
| 95 | + |
| 96 | + // Clone the array |
| 97 | + int[] clonedArray = arr.clone(); |
| 98 | + System.out.println("Cloned array: " + Arrays.toString(clonedArray)); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +> **For more:** [`ArrayExample.java`](./ArrayExample.java) |
0 commit comments