Arrays
Introduction to Arrays
We have seen how to store single pieces of data in variables. What happens when we need to store a group of data? What if we have a list of students in a classroom? Or a ranking of the top 10 horses finishing a horse race?
If we were storing 5 lottery ticket numbers, for example, we could create a different variable for each value:
1 2 3 4 5 |
|
An array hold a fixed number of values of one type. Arrays hold double
s, int
s, boolean
s, or any other primitives. Arrays can also contains String
s as well as object references!
Each index of an array corresponds with a different value. Here is a diagram of an array filled with integer values:
elements | 4 | 8 | 12 | 16 | 20 |
---|---|---|---|---|---|
indices | 0 | 1 | 2 | 3 | 4 |
Similar to C and Python, the indexes start at 0! The element at index 0 is 4
, while the element at index 1 is 8
. This array has a length of 5, since it holds five elements, but the highest index of the array is 4.
Creating an Array Explicitly
Imagine that we're using a program to keep track of the prices of different items we want to buy. We would want a list of the prices and a list of the items they correspond to. To create an array, we first declare the type of data it holds.
double[] prices;
prices = {13.15, 15.87, 14.22, 16.55};
double[] prices = {13.15, 15.87, 14.22, 16.55};
String
s and other objects as well as primitives: String[] clothingItems = {"Tank Top", "Beanie", "Funny Socks", "Pants"};
Importing Arrays
If we want to have a descriptive printout of an array, we need a toString()
method that is provided by the Arrays
package in Java.
import java.util.Arrays;
When we import a package in Java, we are making all of the methods of that package available in our code.
The Arrays
package has many useful methods, including Arrays.toString()
. When we pass an array into Arrays.toString()
, we can see the contents of the array printed out.
1 2 3 4 5 6 7 8 9 10 |
|
[4, 8, 12, 16, 20]
Get Element By Index
Now that we have an array declared and intitialized, we want to be able to get values out of it.
We use square brackets []
to access data at a certain index:
1 2 3 |
|
15.87
. This happens because 15.87
is the item at the 1
index of the array.
If we try to access an element outside of its appropriate index range, we will receive an ArrayIndexOutOfBoundsException
error.
For example, if we were to run the command System.out.println(prices[5])
, we would get the following output:
java.lang.ArrayIndexOutOfBoundsException: 5
Creating an Empty Array
We can also create empty arrays and then fill the items one by one. Empty arrays have to be intitialized with a fixed size:
String[] menuItems = new String[5];
5
. After declaring and intializing, we can set each index of the array to be a different item:
1 2 3 4 5 |
|
1 |
|
menuItems[3] = "Baked cauliflower";
["Veggie hot dog", "Potato salad", "Cornbread", "Baked cauliflower", "Coffee ice cream"]
Array Length
What if we have an array storing all the usernames for our program, and we want to quickly see how many users we have? To get the length of an array, we can access the length
field of the array object:
1 2 |
|
5
, since the menuItems
array has 5
slots, even though they are all empty. If we print out the length of the prices
array:
1 2 3 |
|
4
, since there are four items in the prices
array! String[] args
When we write main()
methods for our programs, we use the parameter String[] args
. Now that we know about array syntax, we can start to parse what that means.
A String[]
is an array made up of String
s. Examples of String
arrays:
1 2 |
|
args
parameter is another example of a String
array. In this case, the array args
contains the arguments that we pass in from the terminal when we run the class file. (So far args
has been empty.) So how can you pass arguments to main()
> Let's say we have this class HelloYou
:
1 2 3 4 5 |
|
HelloYou
in the terminal with an argument of "Laura"
: java HelloYou Laura
Hello Laura
String[] args
would be interpreted as an array with one element, "Laura"
. When we use args[0]
in the main method, we can access that element like we did in HelloYou
. We can actually create if-else
statements that run based on the input:
1 2 3 4 5 6 7 |
|
Arrays Review
We have now seen how to store a list of values in an array. We can use this knowledge to make organized programs with more complex variables.
Throughout these notes, we have leared about:
-
Creating arrays explicitly, using
{
and}
. -
Accessing an index of an array, using
[
and]
. -
Creating empty arrays of a certain size, and filling the indices one by one.
-
Getting the length of an array using
.length
. -
Using the argument
args
that is passed into themain()
method of a class.
Let's create a small program that holds student names and test scores with the following characteristics:
-
Make an array of strings called
students
with the following names: Sade, Alexus, Sam, Koma. -
Create an empty array of
double
s calledmathScores
of size 4. -
Sade got a 94.5 on the test. Store this value in the same indice that she is listed in the
students
array. -
Sam got a 76.8. Store this value in the appropriate spot in the
mathScores
array. -
Finally, add a print statement that says: "The number of students in the class is
numStudents
." using the.length
operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Introduction to ArrayLists
When we work with arrays in Java, we've been limited by the fact that once an array is created, it has a fixed size. We can't add or remove elements.
But what if we needed to add to the book lists, newsfeeds, and other structures we were using arrays to represent?
To create multiple and dynamic lists, we can use Java's ArrayList
s. ArrayList
s allow us to:
-
Store object references as elements
-
Store elements of the same type (just like arrays)
-
Access elements by index (just like arrays)
-
Add elements
-
Remove elements
Remember how we had to import java.util.Arrays
in order to use additional array methods? To use an ArrayList
at all, we need to import them from Java's util
package as well:
import java.util.ArrayList;
Creating ArrayLists
To create an ArrayList
, we need to declare the type of object it will hold, just as we do with arrays:
ArrayList<String> babyNames;
<>
) to declare the type of the ArrayList
. These symbols are used for generics. Generics are a Java construct that allows us to define classes and objects as parameters of an ArrayList
. For this reason, we can't use primitive types in an ArrayList
: 1 2 3 4 5 |
|
<Integer>
generic has to be used in an ArrayList
instead. You can also use <Double>
and <Char>
for types you would normally declare as double
s or char
s. We can intialize to an empty ArrayList
using the new
keyword:
1 2 3 4 5 6 7 |
|
Adding Items to an ArrayList
Now we have an empty ArrayList
, but how do we get it to store values?
ArrayList
comes with an add()
method which inserts an element into the structure. There are two ways we can use add()
.
If we want to add an element to the end of the ArrayList
, we'll call add()
using only one arguement that represents the value we are inserting. In this example, we'll add objects from the Car
class to an ArrayList
called carShow
:
1 2 3 4 5 6 7 8 |
|
ArrayList
, we'll need two arguments in our method call: the first arguement will define the index of the new element while the second argument defines the value of the new element: 1 2 3 4 5 6 7 |
|
Also, note that an error will occur if we try to insert a value at an index that does not exist.
You are able to add multiple data types to the same array using add()
:
1 2 3 4 5 |
|
ArrayList
will be considered Objects
. As a result, they won’t have access to some of their methods without doing some fancy casting. Although this type of ArrayList
is allowed, using an ArrayList
that specifies its type is preferred. ArrayList Size
Let's say we have an ArrayList
that stores items in a user's online shopping cart. As the user navigates through the site and adds items, their cart grows bigger and bigger.
If we wanted to display the number of items in the cart, we could find the size of it using the size()
method:
1 2 3 4 5 6 7 8 9 10 11 |
|
ArrayList
s, it's important to know how to access the amount of objects we have stored. Accessing an Index
With arrays, we can use bracket notation to access a value at a particular index:
1 2 3 4 |
|
5.5
, the value at index 1
of the array. For ArrayList
s, bracket notation won't work. Instead we use the method get()
to access an index:
1 2 3 4 5 6 7 |
|
"HK-416 Assault Rifle"
, which is the value at index 2 of the ArrayList
. Changing a Value
When we were using arrays, we could rewrite entries by using bracket notation to reassign values:
1 2 3 4 |
|
ArrayList
has a slightly different way of doing this, using the set()
method:1 2 3 4 5 6 7 8 9 |
|
Removing an Item
What if we wanted to get rid of an entry alltogether? For arrays, we would have to make a completely new array without the value.
Luckily, ArrayList
s allow us to remove an item by specifying the index to remove:
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 |
|
Getting an Item's Index
What if we had a really large list and wanted to know the position of a certain element in it? For instance, what if we had an ArrayList
detectives
with the names of fictional detectives in chronological order, and we wanted to know what position "Flecther"
was.
1 2 |
|
4
, since "Fletcher"
is at index 4
of the detectives
ArrayList
. ArrayLists Review
Some crucial methods in ArrayList
s:
-
Adding a new ArrayList item using
add()
. -
Accessing the size of an ArrayList using
size()
. -
Finding an item by index using
get()
. -
Changing the value of an ArrayList item using
set()
. -
Removing an item with a specific value using
remove()
. -
Retrieving the index of an item with a specific value using
indexOf()
.