1. What is an ArrayList, how is it different than an array?
2. How to use an ArrayList to store and iterate data.
3. Know and use the most useful ArrayList methods.
An ArrayList is a resizable-array implementation of the List interface that implements all list operations and permits all elements. This class still uses an array, but adds useful methods that handle all of the shortcomings of an array. For instance, an array has a fixed size and did not easily allow for inserting of elements. An ArrayList can change in size and so much more.
1. import the ArrayList class. java.util.ArrayList
2. Instantiate an ArrayList*
a. the Type: ArrayList<E> **
b. the Name: the name you want for you ArrayList.
c. assignment operator =
d. new keyword
e. ArrayList<E>
f. ();
3. Then we can fill & use our ArrayList.
*E should be replaced with the type of element you want. For primitives, you’ll need to use the wrapper classes: Integer, Double, Boolean, etc. For objects/instances, you’ll need to use the class name: String, or the name of the class.
** Because the ArrayList implements the List interface, you can set the original type to be either.
There are several useful methods that we get when we use an ArrayList instead of an Array. Here are some to start:
1. add(E element);
This adds a value, that needs to be the type that we set in between the angle brackets <>, at the next available place.
2. get(int index);
This retrieves the value at a specific index.
3. size();
This returns the size of the ArrayList. This value can change with each additional element added.
4. remove(int index);
This removes a specific element at the index you want, every element after this index will slide down so there are no gaps in the ArrayList.
Here is the good news, if you want to print out all the values of an ArrayList, you can just print it by name!
Hmmmm.. Wouldn’t this have printed out the place in memory before? I wonder what method they made in the ArrayList to do this…. :]
The shortcoming of this method is that it prints it out in a specific format that we do not get to change, what if we want our own formatting.
In order to have our own formatting, we can use a foreach loop, but that’ll only allow us to format the way it is printed. Instead of having the entire collection separated by commas on the same line. We can slightly alter with the for each loop.
If we want a little more control of the way an ArrayList is printed or if we want to do further manipulations, we can use a standard for loop.
This will need the .size()
& .get(int index)
method.
There are a couple more really useful methods that we could want to use:
1. add(int index, E element);
adds an element at a position you want. Slides all other elements down.
2. indexOf();
returns the first index of a specific type you are looking for.
3. set(int index, E element);
this replaces an element at a position, the same amount of elements is retained.
Another useful feature is setting the type to Object, this allows us to store mixed data types in our Arrays instead of a single type; this is because all classes inherit from the class Object. Another useful feature over Arrays that can only be one type. This comes with its own complications. Now all the data is of type Object. So if I know the 1st index is an integer and I try to store it in an int variable, I will receive an error.
This is where the instanceof keyword comes in hand, we change check if an Object is really an instance of another class. If it is for the type we are looking for, then we can cast that object to the correct type and store it in a variable.
Youtube Link: More ArrayList Methods
The ArrayList is really using an Array, but ArrayLists are dynamically sized while Arrays are fixed size. How does this work?
When adding an element, the ArrayList checks how much space is left and then will create a new array of doubled size. This gives the appearance that it can be any size, but really it is just constantly adjusting its own size.
1. Ask the user what data they are trying to store: String, double, int.
2. Instantiate an ArrayList for that type, and then let them continue to enter values until they decide to stop by entering an agreed upon keyword for stopping (letting the user choose).
3. After the user finishes entering their values, print their list.
4. Give them options for removing values, setting values at specific places, or adding more. Reprint after each change.
1. You'll need to download the pre-code for this example.
2. Ask the user how large they want the list to be and send it to the generateList() method. This will randomly pull data of different types into an Arraylist in the main method.
3. Loop through the ArrayList and print out what the element is and what data type it is!
1. There is pre-code for this example. This program includes code from the under the hood video for how we can add an element and make an Array dynamically sized.
2. I want you to write the implementation for several methods.
3. Insert(int index, E element); Your code should insert the element at that specific index, and then slide all other elements up one index.
4. Remove(int index); Your code should remove the element at that specific index, and then slide all the other elements down one index.