Lesson Weekend

In Intro to Programming we worked extensively with arrays. After all, they're pretty powerful: You can group things together, loop through them, and manipulate fairly large amounts of data at once! In programming, approaches to organizing pieces of related information are known as data structures.

In Java, the basic idea of an array is similar: It's essentially a collection of multiple items. But the rules are actually quite different. (Pay careful attention to ensure you don't miss any of these differences!) In this lesson we'll briefly walk through the differences and similarities of Java arrays, and the JavaScript arrays we're already familiar with. After that, we'll practice creating, accessing, and manipulating our own custom arrays.

Array vs. ArrayList

The arrays we used in JavaScript were fairly flexible. We could store a variety of different things in them, and their size could easily fluctuate as we added or removed items. However, what we know as an Array in JavaScript is actually two different things in Java. And there are serious differences between the two.

Java includes both an Array and an ArrayList class. They're each "collections of multiple items", but they're also very, very different. Pay careful attention.

  • An ArrayList is similar to arrays we used in Intro to Programming. They can fluctuate in size as items are added or removed, and a single ArrayList may hold a wide variety of different items in the same list.

  • An Array is stricter. They have a set amount of items (their length absolutely may not grow or shrink in size), and all items stored in the list must be of the same type (such as String, orInteger`). After an Array is created, you cannot add or remove objects to make it larger or smaller.

This lesson will focus solely on Java Array objects. We'll cover ArrayList in depth later on.

Arrays

We can create an array containing strings in our REPL like this:

> String[] fruits = { "apples", "bananas", "oranges", "grapes" };
java.lang.String[] fruits = ["apples", "bananas", "oranges", "grapes"]

Alternatively, we can create an array of integers like this:

> Integer[] primeNumbers = {  2, 3, 5, 7, 11 };
java.lang.Integer[] primeNumbers = [2, 3, 5, 7, 11]

There are several steps to create a Java Array:

  1. Determine what type of data the array will contain. Again, data in Java arrays must be the same type. Determine what data type your array will hold. As you can see, we clearly state String when creating an array of fruit names above, and Integer when creating an array of numbers.

  2. Append square brackets [] to the data type declaration. These allow us to declare that the object we're about to create will be an Array. In the example above, Integer[] means an array of integers. String[] represents an array of strings. If we had a class named Cat, we would begin to declare an array of cats by stating Cat[]. There should be no spaces between the class name and the square brackets.

  3. Include a variable name. After declaring the array and the type of data it will hold, include a name. Like String[] fruits, or Integer[] primeNumbers.

  4. Assign data to the array. We use the assignment operator = to assign the array variable a list of information. Note that this information is contained within {}, or curly brackets, not the square brackets we previously used when creating arrays in JavaScript. Also, remember that the length of the array may not change after you assign it.

Accessing Array Data

After creating an array, we'll usually want to access the information later on. Thankfully, this is very straightforward in Java. Let's assume we have created an array containing the names of various fruits:

> String[] fruits = { "apples", "bananas", "oranges", "grapes" };
java.lang.String[] fruits = ["apples", "bananas", "oranges", "grapes"]

We can later access the entire array by simply calling the array's variable name:

> fruits
java.lang.String[] fruits = ["apples", "bananas", "oranges", "grapes"]

To access a particular entry we append square brackets, [], to the array's variable name, and inside the square brackets we insert the index of the item that we want to reference. The array will then return the information that we asked it for:

> fruits[1];
java.lang.String res0 = "bananas"

> fruits[3];
java.lang.String res1 = "grapes"

> String favoriteFruit = fruits[0];
java.lang.String favoriteFruit = "apples";

Remember that Java counts list items starting at 0. What normal humans would call the first item in an array is actually at index 0, the second is at index 1, and the one hundredth is at index 99.

Altering Array Data

Again, we cannot change the length of an array. However, we can change individual values within the array, as long as the overall length is not altered. For instance, we know the item in the 2nd index of our array is "oranges":

> fruits
java.lang.String[] fruits = ["apples", "bananas", "oranges", "grapes"]

Let's say we want to be more specific, and change this value to "blood oranges". We can do this by redefining the item at that index, like so:

> fruits[2] = "blood oranges";
java.lang.String res2 = "blood oranges"

> fruits
java.lang.String[] fruits = ["apples", "bananas", "blood oranges", "grapes"]

We already know we can call fruits[2] to access the second index of our fruits array. This time, we just include the = assignment operator to assign the second index to a new value.

This can be done for any index of the array:

> fruits[3] = "concord grapes";
java.lang.String res2 = "concord grapes"

> fruits
java.lang.String[] fruits = ["apples", "bananas", "blood oranges", "concord grapes"]

In the next lesson we'll cover looping through arrays to access every item of their contents individually.