Tuesday, September 12, 2023

 Arrays


Intro


Array is a special type of data structure, where we have easy access to elements. It is an index-based data structure and elements get stored in a contiguous order in memory, hence the fast retrieval.


There are two types of Arrays:
  1. Static - (Fixed-size Array) - an array for which the size or length is determined when the array is created and/or allocated and we can not change it afterward.
  2. Dynamic - With dynamic arrays, we have the option to add more elements than specified while creating it. In most languages, we have classes that represent the dynamic arrays. In Java, it's the ArrayList class.

In this post, we will cover the static arrays.

Static arrays


Let's go through some examples of working with arrays in Java.


We can create an array in Java in several ways:


public class DevPrimer {

public static void main(String[] args) {

int[] arr = new int[5]; // create an array with the value of 5

int[] arr1 = {}; // create an empty array

int[] arr2 = new int[]{}; // also creates an empty array


}

}

As you can see, we can create an array (arr) and immediately provide its length.
With the arr1, we are using array literal. And we created an empty array, so it is not possible to add any element to it later on.
The same for the arr2.


Here is an example where we create and initialize an array with some values:


public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};


}


Here, we added five elements while creating an array, which means we allocated the size of 5 and we can not add any more elements.

The elements got placed in contiguous order in memory - next to each other. Each element has its index that we can use to access it from the code.


Access the element at index 2:


public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

System.out.println(arr[2]);
}

Output: 3.


Let's replace the element at index position 3:


public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

arr[3] = 7;

// print the array
Arrays.stream(arr).forEach(element -> System.out.print(element + " "));
}

Output: 1 2 3 7 5.


Let's try adding one more element:


public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

arr[5] = 12;
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at DevPrimer.main(DevPrimer.java:12)


We got an exception since we tried to add one more element at index position 5, and we have indexes only up to number 4. So this is not possible with static arrays.


An array is an object in Java, so we can not just print it like this:


public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

System.out.println(arr);
}

Output: [I@4f3f5b24

In order to print the values of the array, we need to iterate over it. We can do it in several ways:


public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5};

// traditional for-loop
for(int i = 0; i< arr.length;i++) {
System.out.print(arr[i] + " ");
}

// enhanced for-loop
for(Integer i : arr) {
System.out.print(i + " ");
}

// Java Streams (we use the Arrays class to wrap the array into a Stream and
// then to print the elements using the forEach terminal operation)
Arrays.stream(arr).forEach(item -> System.out.print(item + " "));
}



Static array - analysis


Since we can access the elements by the index position, the time complexity of retrieval or accessing the element is O(1).

Adding an element is O(1), while iterating over an array is O(n).





No comments:

Post a Comment