AlgoCademy
Lesson
Code

Arrays in {lang}


An array is a data structure consisting of a collection of elements, each identified by at least one array index or key.

In non-formal words, we use arrays to store several pieces of data in one place.

There are two types of arrays: static and dynamic.

Static arrays

A static array or fixed array is an array for which the size or length is determined when the array is created and/or allocated.

Although most of the time we use dynamic arrays, we recommend learning about how static arrays work to get a better idea why dynamic arrays are so powerful. And we prepared a video for you:


Creation:

Let's see how we can create a static array of size 10 that will hold Strings:

String[] sandwitch = new String[10];

You can also create a static array and initialize it with some values:

String[] sandwich = {"peanut butter", "jelly", "bread"};

Random access:

We can access the data inside arrays using indexes and the [] operator. Arrays use zero-based indexing, so the first element in an array has an index of 0.

int[] numbers = {50, 60, 70};
int data = numbers[1];

// numbers[0] is 50, and data has the value 60

Changing elements:

The entries of an arrays are mutable and can be changed freely, using indices and the [] operator.

int[] ourArray = {50, 40, 30};
ourArray[0] = 15;

// ourArray now has the value [15, 40, 30]

Getting the size:

We can get the size of an ArrayList using the .length attribute.

int[] ourArray = {50, 40, 30};
System.out.println(ourArray.length);

// This will print 3

All the operations above are considered to take O(1) time to be executed.

Iterating through the elements:

We can iterate throught the elements of an array using a for loop and the : operator like this:

String[] sandwich = {"peanut butter", "jelly", "bread"};
for (String ingredient : sandwich) {
    System.out.println(ingredient);
}

// This will print the strings "peanut butter", "jelly" and "bread" on three separate lines

We can also iterate throught the elements of an array using indices and a for loop like this:

String[] sandwich = {"peanut butter", "jelly", "bread"};
for (int i = 0; i < sandwich.length; i++) {
    System.out.println(sandwich[i]);
}

// This will print the strings "peanut butter", "jelly" and "bread" on three separate lines

Both ways take O(n) time, where n is the length of the array we iterate on.


Dynamic arrays

A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed.

Dynamic arrays overcome a limit of static arrays, which have a fixed capacity that needs to be specified at allocation.

In Java, the dynamic array is implemented as ArrayList. The length of a Java ArrayList is not fixed, but all its elements should be of the same type, which is specified at allocation.

Creation:

We create an ArrayList by using the keywords new and ArrayList, then writing the type of the elements inside <> followed by paranthesis, like this:

new ArrayList<String>();

A good practice is to cast the created ArrayList in its interface by using a List variable and specifying there the type of the elements, like this:

List<String> sandwich = new ArrayList<>();

You can also initialize an ArrayList at creation by using the List.of() function and put a comma between each entry, like this:

List<String> sandwich = new ArrayList<>(List.of("peanut butter", "jelly", "bread"));

Random access:

We can access the data inside ArrayLists using indexes and the .get() function. ArrayLists use zero-based indexing, so the first element in an ArrayList has an index of 0.

List<Integer> array = new ArrayList<>(List.of(50, 60, 70));
int data = array.get(1);

// array[0] is 50, and data has the value 60

Appending:

An easy way to append data to the end of an ArrayList is via the .add() function.

.add() takes one parameter and "pushes" it onto the end of the ArrayList.

Examples:

List<Integer> arr1 = new ArrayList<>(List.of(1, 2, 3));
arr1.add(4);

// arr1 now has the value [1, 2, 3, 4]

Removing:

Another way to change the data in an ArrayList is with the .remove() function.

.remove() takes one parameter which represents an index and removes the element placed at that index.

List<Integer> threeArr = new ArrayList<>(List.of(1, 4, 6));
threeArr.remove(1);

// threeArray has now the value [1, 6];

Changing elements:

The entries of ArrayLists are mutable and can be changed freely, using indices and the .set() function

.set() takes two parameters which represent an index and a value and changes the element placed at that index with the argument value.

List<Integer> ourArray = new ArrayList<>(List.of(50, 40, 30));
ourArray.set(0, 15);

// ourArray now has the value [15, 40, 30]

Getting the size:

We can get the size of an ArrayList using the .size() function.

List<Integer> ourArray = new ArrayList<>(List.of(50, 40, 30));
System.out.println(ourArray.size());

ourArray.remove(2);
System.out.println(ourArray.size());

// This will print 3, then 2

All the operations above, except for remove, are considered to take O(1) time to be executed.

remove takes O(n) time in the worst case, where n is the length of the array.

Removing the last element in an ArrayList takes O(1) time tho.

Iterating through the elements:

We can iterate throught the elements of an ArrayList using a for loop and the : operator like this:

List<String> sandwich = new ArrayList<>(List.of("peanut butter", "jelly", "bread"));
for (String ingredient : sandwich) {
    System.out.println(ingredient);
}

// This will print the strings "peanut butter", "jelly" and "bread" on three separate lines

We can also iterate throught the elements of an ArrayList using indices and a for loop like this:

List<String> sandwich = new ArrayList<>(List.of("peanut butter", "jelly", "bread"));
for (int i = 0; i < sandwich.size(); i++) {
    System.out.println(sandwich[i]);
}

// This will print the strings "peanut butter", "jelly" and "bread" on three separate lines

Both ways take O(n) time, where n is the length of the array we iterate on.

How the dynamic array works behind the scenes is not trivial and you might be asked to explain this in interviews. So make sure you watch this video to fully understand how the dynamic array operates:



Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.