☰ Contents Collections and Generics

ArrayList Intro

Student: Professor, something has been annoying me about arrays. Sometimes when making an array, I don’t yet know how many elements I would like to store in it. Should I just make the array be really long? That seems like it would be a waste of space.
Prof: You’re right, creating a really long empty array is a bad idea in many cases. That is a limit of arrays: whenever we want to make an array longer, we have to make a whole new array and copy all the data over. That is pretty annoying. Here, let me introduce you to a class that Java provides to us to help with this issue. Java provides us with collections, data structures for storing a collection of objects. One of these collections is ArrayList.
Prof: What do you notice from this code?
Student: You are creating a new ArrayList object.
Prof: Correct. Based on the definition, what type of data is this ArrayList called names storing?
Student: It looks like String, but it’s using notation we haven’t talked about before, it has String inside of arrows?
Prof: We will go over the specifics of that notation later, for now you are correct that it is storing elements of type String.
Student: Ok, and then after that we are adding names to the ArrayList names.
Prof: Notice that add() is a method. We can’t use names[index] = "Ada", that isn’t valid code. When we use add(), it appends the element to the end of our ArrayList.
Student: And whenever we append a new element, that means that the ArrayList is now one element longer. So can I use names.length like I do with arrays?
Prof: It’s different with ArrayList, you instead check using a method called size(). You also access elements with a method as well: get(), which allows you to fetch an element at a specific index, similar to an array.
Student: But what if I don’t want to add an element at the end? Or what if I want to replace one of the names in the list?
Prof: There are other methods you can use, such as add(index, value) and set(index, value). And you can use remove(index) to get rid of elements as well.
Prof: When you remove an element at index 0, and don’t replace it, what would you get if you tried to use get(0)?
Student: Well that index would be empty, so it wouldn’t contain a String object. In that case it must be null!
Prof: That’s a good guess if this were an array, but an ArrayList never leaves a gap. When you remove an element, everything after it shifts down one index to close the gap, and size() goes down by one. So get(0) gives you the element that used to be at index 1. Likewise, add(index, value) shifts the later elements up to make room.
Prof: Now ArrayList also comes with some other helpful methods. One of the most useful is contains(), which tells you whether or not the ArrayList contains a certain element. Since duplicate values are allowed in an ArrayList, all it tells you is that the element is in the ArrayList at least once.
Student: Hey Professor? I just tried to make an ArrayList of type int, but it doesn’t seem to be working.
Prof: Collections in Java, such as an ArrayList, can only store objects. You can’t put primitives in a collection.
Student: But what if I want to store a list of whole numbers?
Prof: You can still do that, Java has a version for each primitive that is an object instead. For int you can instead use Integer, and Java turns an int like 3 into an Integer for you when you add it. Try re-writing your code with that instead.
Prof: There you go. Remember that ArrayList is ordered like standard arrays, and that the first element is index 0. But unlike an array their size can be changed when using add() and remove(). From here, we will talk about some other types of collections in Java that are less like arrays.

Practice

names starts as [Ada, Alan]. Fill in the three missing pieces so that it ends as [Grace, Alan]: insert "Grace" at index 1, then remove the element at index 0, then record how many elements are left.
import java.util.ArrayList;

ArrayList<String> names = new ArrayList<String>();
void main() {
names.add("Ada");
names.add("Alan");


int count = ;
System.out.println(names);
System.out.println(count);
}