





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science โ University of San Francisco โ p.1/
Last time, we learned about how to use arrays in Java. Java also provides an ArrayList class that can helpmanage arrays. ArrayList is a generic container. That means that we can use the same container tostore different kinds of elements. Department of Computer Science โ University of San Francisco โ p.2/
Notice that we declared an initial array size. The array is then able to grow dynamically beyond that. Itโs better to allocate in advance if we can. We can also remove elements, and access them. We can also add in the middle of a list: band.add(1, โโringoโโ); Department of Computer Science โ University of San Francisco โ p.4/
get(index) lets us access the element at a particular index. Elements in an ArrayList are stored as Objects
This means that we need to cast them back to Strings. Canโt store primitives. String name = (String)band.get(2); Department of Computer Science โ University of San Francisco โ p.5/
also: size() - tells us how many elements are in the array. isEmpty() - tells us whether thereโs anything in the list. contains(Object o) - tells us whether an object is in thelist. clear() - removes everything. Department of Computer Science โ University of San Francisco โ p.7/
If we want to avoid casting elements, we can also definethe ArrayList to accept particular sorts of objects: Java 1.5 only ... ArrayList