The Java Parrot — Working with many things, with Array, and ArrayList

ShipItParrot
4 min readDec 19, 2022

--

Ahoy parrot!

In our previous articles, we explored two important concepts;

  • Data Types (Primitives, and Reference Types)
  • Conditional statements.

This lets us express data in Java, and do stuff based on conditions!

Now, we will build on top of that by learning how we can work with many items.

Lets explore the simplest data structure in Java — Array, and ArrayList

The Array

Arrays are a collection of items! These string arrays are allocated a fixed, continuous memory block, and we can’t expand or shrink it.

It’s analogous to a tray of seeds!

You can have an array of all the different types of seeds you love to eat, an array of your favorite perching spots, or an array of your hoomans’ names (in case you need to peck them for not giving you enough seeds).

Creating an Array

We can use curly brackets {} and separate the items with commas. For example:

// Creating an Array of seeds
String[] seeds = {"sunflower", "millet", "safflower", "canary"};

// Creating an Array of perching spots
String[] perchingSpots = {"tree branch", "birdhouse", "window ledge", "bird feeder"};

// Creating an Array of hoomans
String[] hoomans = {"John", "Jane", "Joe"};

If we are unsure of what the names of our hoomans are, we can also define a placeholder Array with the new keyword, followed by `String[3]` to give it a size of 3.

String[] hoomans = new String[3];

This will create an array with 3 elements, all of which are initially set to null. null is a placeholder value, representing nothing! We can then populate it later!

Getting items in an Array by index

We can access the items in a Array using their index. The index starts at 0, so to access the first item in the Array, you use the index 0. For example:

// Creating an Array of seeds
String[] seeds = {"sunflower", "millet", "safflower", "canary"};

// Creating an Array of perching spots
String[] perchingSpots = {"tree branch", "birdhouse", "window ledge", "bird feeder"};

// Creating an Array of hoomans
String[] hoomans = {"John", "Jane", "Joe"};

System.out.println(seeds[0]); // prints "sunflower"
System.out.println(perchingSpots[2]); // prints "window ledge"
System.out.println(hoomans[1]); // prints "Jane"

Changing an item in an Array by index

We can also change the value of an item in an array by using its index and assigning a new value to it. For example:

// Creating an Array of seeds
String[] seeds = {"sunflower", "millet", "safflower", "canary"};

seeds[2] = "linseed";
System.out.println(Arrays.toString(seeds)); // prints "[sunflower, millet, linseed, linseed]"

Arrays are fixed in size! Annoying!

It can be annoying working with Arrays! We can’t easily increase or decrease its size.

You smart parrots would point out:
“Can’t we just create a new Array with one size larger?”

Like this:

String[] seeds = {"sunflower", "millet", "safflower", "canary"};

// Create a new array with size one greater than the old array
String[] newSeeds = new String[seeds.length + 1];

// Copy the elements from the old array to the new array
for (int i = 0; i < seeds.length; i++) {
newSeeds[i] = seeds[i];
}

// Add the new element to the end of the new array
newSeeds[newSeeds.length - 1] = "pumpkin";

// Replace the old array with the new array
seeds = newSeeds;

System.out.println(Arrays.toString(seeds)); // prints "[sunflower, millet, safflower, canary, pumpkin]"

Yes we can, but it is slow and expensive! The time taken will scale linearly wih the size of the original list!

Luckily for us parrots, we have a dynamic-size Array, the ArrayList!

The ArrayList

Its just like an Array, but dynamically sized! We can add items to it, or remove items from it.

Create an ArrayList

We can define an ArrayList object by calling its constructor.

import java.util.ArrayList;

ArrayList<String> seeds = new ArrayList<String>();

// You can also specify the initial capacity of the ArrayList
ArrayList<String> seeds = new ArrayList<String>(10);

Add elements to an ArrayList

we can use its add method:

ArrayList<String> seeds = new ArrayList<>();
seeds.add("sunflower");
seeds.add("millet");
seeds.add("safflower");
seeds.add("canary");

Get items in an Array by index

To access an element in an ArrayList, we can use the get method and specify the index of the element we want to retrieve:

ArrayList<String> seeds = new ArrayList<>();
seeds.add("sunflower");
seeds.add("millet");
seeds.add("safflower");
seeds.add("canary");

System.out.println(seeds.get(0)); // prints "sunflower"
System.out.println(seeds.get(2)); // prints "safflower"

Change an item in an Array by index

To change the value of an element in an ArrayList, we can use the set method and specify the index of the element we want to change and the new value:

ArrayList<String> seeds = new ArrayList<>();
seeds.add("sunflower");
seeds.add("millet");
seeds.add("safflower");
seeds.add("canary");

seeds.set(2, "linseed");
System.out.println(seeds); // prints "[sunflower, millet, linseed, canary]"

Remove an item in an array by index

To remove an element from an ArrayList, you can use the remove method and specify the index of the element you want to remove:

ArrayList<String> seeds = new ArrayList<>();
seeds.add("sunflower");
seeds.add("millet");
seeds.add("safflower");
seeds.add("canary");

seeds.remove(2);
System.out.println(seeds); // prints "[sunflower, millet, canary]"
  • Be careful! This can take a lot of time; an ArrayList needs to move all items that comes after “safflower” in the ArrayList to the left, after removing it.
  • In the worst case, if we are provided the first item; “sunflower”, it will end up spending time shifting as many items as there are in the ArrayList; time taken to run this scales linearly with the size of the ArrayList
  • Similarly, we say that this method has a worst-case time-complexity of O(N), where N is the size of the ArrayList!

A small detail (and why we will explore another data structure; Linked List later):

  • Items in an ArrayList, are stored in a continuous block of memory.
  • When there are more items than what its current continuous block of memory can store, ArrayList resizes itself by copying its contents into a larger continuous block of memory.

We will be using Array and ArrayList a lot from here, to build cool and efficient data structures which can do way more stuff than what they can do!

See you in the next article!

--

--