Library books, or are we talking "Arrays"?

[ Data Structures ] Arrays – what about them?

·

4 min read

Imagine you are in a library, facing rows of bookshelves. Each bookshelf is stacked with rows and rows of books covering many topics–History, Astronomy, Biography, and Literature–like this one I got from Google:

library.jpeg

Because the libraries have to handle thousands of books daily, librarians and readers need to agree with each other on how to find those books–if they are on the shelves–or how to check for them if they are available but stored somewhere else. The libraries often have specific rules for new books or missing ones. If you have gone to libraries, it's a fascinating place, but it can be daunting to keep books in check.

One common thing that most library books have is a code. The codes often hold information of the books' genre and author, and looking at these codes you'll know where to find the books themselves. I personally think this is really helpful, although I doubt that it'll be the last system human brains come up with to organize library books.

Such rows of books can be a perfect example of an array. Each book has a code, or an "index," that points librarians and readers to the right place.

If you have no problem imagining the bookshelves, let's see if you can imagine this.

Arrays

What about them?

An array is like a one-row bookshelf. It stores similar data types–strings, numbers, boolean, or even null and undefined, and each individual in the array has an index that points to it. For example:

import java.util.Arrays;

public class App {
    public static void main(String[] args) throws Exception {
        String[] arr = {"h", "e", "l", "l", "o"};
        System.out.print(Arrays.toString(arr)); //[h, e, l, l, o]
    }
}

Depending on the language, an array can be static or dynamic, Which means we cannot change the number of elements an array has for the former but we can add more elements to the latter. The example above used Java to print an array of 5 elements, each element is a string, and the array is static. If I want to change the size of the array–if I want to add "!" at the end of arr–then I'll have to create a new array with a size of 7 + 1 = 8 and add the new string.

import java.util.Arrays;

public class App {
    public static void main(String[] args) throws Exception {
        String[] arr = {"h", "e", "l", "l", "o"};
        int n = arr.length;
        String[] newArr = new String[n + 1];
        for (int i = 0; i < n; i++) {
            newArr[i] = arr[i];
        }
        newArr[n] = "!";
        System.out.print(Arrays.toString(newArr)); // [h, e, l, l, o, !]
    }
}

It is possible to add elements, it's just as convenient as languages like Javascript or Python do.

Creating a new array every time you want to add an element is a lot of work. It's like you have this bookshelf that's exactly the size of your current amount of books, or of the number of books you imagine your library can have. As time flies, the world continues on to nurture so many writers that there are new books every day. So if you want to add those books, you'll have to make a new bookshelf and tediously carry one book from the old shelf to the new one.

I bet there are better ways to do this, but you get the idea of an array, don't you?

What about dynamic arrays? Are they any better? It's hard to say which is better than which, but as I aforementioned, it's more convenient. Here's a code snippet for creating and adding elements to an array in Javascript.

Creating an array

function createArray() {
      let arr = ["h", "e", "l", "l", "o"];
      return arr;
}

console.log(createArray());

Adding a new element to the array

function addToArray() {
      let arr = ["h", "e", "l", "l", "o"];
      arr.push("!");
      return arr;
}

console.log(addToArray());

Done! You now know how to create and change the size of an array.

However, at this point, we haven't thought about the "code" for each of the array elements, like the "code" for each book on a bookshelf yet. Most languages are 0-indexed, which means their array starts with element 0 and counting onwards until the last element. We can access this element using arrayName[index].

In the java example ["h", "e", "l", "l", "o"], arr[0] gives us h, arr[1] gives us e, arr[3] gives us l, and so on. When we added value from arr to newArr, we point to each index and assign a value to that index location, arr[n] = "!". This means that as long as we know the index of an element, we can access its value easily.

Huray! We have gotten through the basics of an array. We'll move on to time and space complexity in the next post. Stay tuned.