What is Array?
An array is a data structure that allows you to store multiple elements of the same data type in a contiguous block of memory. Each element can be accessed using an index.
Arrays are dynamically created objects in java which means →
Created at Runtime
Arrays in Java are created dynamically during program execution (at runtime) using the
new
keyword. You don't need to specify the elements at compile time.int[] numbers = new int[5]; // Array object created dynamically
Stored in the Heap
Since arrays are objects in Java, they are always stored in heap memory, even if the reference to the array is stored in the stack.
Object-Like Behavior
Being objects, arrays have properties:
You can access their length using
.length
.They are passed by reference, just like other objects.
Variable Size at Creation
import java.util.Scanner; public class DynamicArrayExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the array size: "); int size = scanner.nextInt(); int[] numbers = new int[size]; // Size determined at runtime System.out.println("Array of size " + numbers.length + " created."); } } //The size of an array does not have to be a constant. It can be determined dynamically at runtime based on program logic.
Arrays Stored in Heap memory or Stack memory ?
So We know Arrays are stored in Heap memory as it is Object in java Also we know Heap memory is not contiguous/continious still how can we say array being stored in Heap memory is still allocated continious block of memory?
The answer is simple JVM manages and is responsible for space allocation
Two ways of creating arrays in java →
- Declaration followed by initialization
int [ ] array1 ={1,2,50,945,64,84,86};
Datatype [ Symbol of Array is square bracket ] variable_name ={values of same data type } ; this is called declaration and initialization as we are declaring variable and initializing it too .Even this is stored in heap memory as arrays are object in java
- First Declaration and then initialization
String [ ] array2 = new String [ 4 ] ;
This create an array which can hold 4 integer type numbers inside variable array2.This is usually helpful if Programmer wants to accept input from the user
Methods from the Arrays Class
Arrays.sort(arr)
– Sorts the array in ascending order.Arrays.toString(arr)
– Converts the array to a string for easy printing.Arrays.binarySearch(arr, key)
– Searches for the elementkey
in a sorted array.Arrays.fill(arr, value)
– Fills the entire array with the specified value.Arrays.equals(arr1, arr2)
– Compares two arrays for equality (both in content and size).Arrays.copyOf(arr, newLength)
– Creates a new array that is a copy of the original array with a new specified length.import java.util.Arrays; public class ArrayMethodsExample { public static void main(String[] args) { // Declare and initialize an array int[] arr = {10, 20, 30, 40, 50}; // 1. Accessing Array Elements System.out.println("First element: " + arr[0]); // Access first element System.out.println("Last element: " + arr[arr.length - 1]); // Access last element // 2. Modifying Array Elements arr[2] = 35; // Change element at index 2 System.out.println("Modified array: " + Arrays.toString(arr)); // 3. Array Length System.out.println("Array length: " + arr.length); // Get array length // 4. Arrays Class Methods // Sorting the array Arrays.sort(arr); System.out.println("Sorted array: " + Arrays.toString(arr)); // Sort the array // Searching for an element int index = Arrays.binarySearch(arr, 35); // Binary search (array must be sorted) System.out.println("Index of 35: " + index); // Filling an array with a specific value Arrays.fill(arr, 100); // Fill entire array with 100 System.out.println("Filled array: " + Arrays.toString(arr)); // Checking equality of arrays int[] arr2 = {100, 100, 100, 100, 100}; boolean areEqual = Arrays.equals(arr, arr2); // Check if two arrays are equal System.out.println("Arrays are equal: " + areEqual); // Copying an array int[] arrCopy = Arrays.copyOf(arr, arr.length); System.out.println("Copied array: " + Arrays.toString(arrCopy)); } }
Traversal in Array
- using for-loop →it uses the index of arrays to print the element
import java.util.Arrays; public class ArrayMethodsExample { public static void main(String[] args) { // Declare and initialize an array int[] arr = {10, 20, 30, 40, 50}; //For-loop for( int i=0;i<arr.length;i++){ System.out.println(arr[i]); } }
2) using For-each loop
For-each loop assigns a variable of same data types as stored in array and we keep assigning the values of the array one by one from 0 index of array to this variable
i
and thus directly printing the value
import java.util.Arrays;
public class ArrayMethodsExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] arr = {10, 20, 30, 40, 50};
for(int i : arr){
System.out.println(i);
}
}
3)using toString( ) method From Array class
This way we convert our array to array of type String and print it like we print normal strings
import java.util.Arrays;
public class ArrayMethodsExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] arr = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arr));
}
}