ArrayList Methods
Methods you need to know!: 7.2.0
int size()
- returns the number of elements in the list
boolean add(E object)
- appends
object
to the end of the list; returns true
- appends
E remove(int index)
- removes an item at an index; shifts remaining items to the left
void add(int index, E obj)
- inserts
obj
at index - moves any current objects at or beyond
index
to the right
- inserts
E get(int index)
- returns the item at
index
- returns the item at
E set(int index, E obj)
- overwrites value at
index
- overwrites value at
Size(): 7.2.1
- You can get the number of items in a list using
.size()
- An empty arraylist has a
.size()
of 0
- An empty arraylist has a
- This is different from the
.length
member variable of an array- You will not be penalized for mixing these two up on the exam, but they are different
Add(obj) to an ArrayList: 7.2.2
ArrayList<String> shoppingList = new ArrayList<String>();
shoppingList.add("Eggs");
shoppingList.add("Spam");
shoppingList.add(0, "Coffee"); // Java is important ;)
// You can use an index in the add() method!
// This shifts Eggs and Spam to index 1 and 2 respectively
Primitive/Object conversion
When adding a primitive type like int
to a list, you can use an integer constructor .add(new Integer(5))
. However, you can also add the value directly (.add(5)
) as the value will be automatically converted to an Integer
in a process called autoboxing
. Taking an int
out of this list is known as unboxing
. Calling the constructor is valid in Java 7 (the version used on the exam), but has been deprecated in Java 9. Using autoboxing is always preferred, and is valid in all Java versions.
Remove(index) from ArrayList
ArrayList<String> shoppingList = new ArrayList<String>();
shoppingList.add("Eggs");
shoppingList.add("Spam");
shoppingList.add(0, "Coffee");
System.out.println(shoppingList); // [Coffee, Eggs, Spam]
shoppingList.remove(0); // we buy coffee; the other items are shifted up an index
System.out.println(shoppingList); // [Eggs, Spam]
ArrayList get/set Methods: 7.2.5
ArrayList<String> shoppingList = new ArrayList<String>();
shoppingList.add("Eggs");
shoppingList.add("Spam");
shoppingList.add(0, "Coffee");
System.out.println(shoppingList); // [Coffee, Eggs, Spam]
for (int i = 0; i < shoppingList.size(); i++) {
System.out.println(shoppingList.get(i));
}
shoppingList.set(0, "Java"); // That's better :)
Arrays vs ArrayLists: 7.2.6
- Use an array when you know how many items you want to store
- The order won’t change after initialization
- You want something more rigid (this is good sometimes!)
- Use an arraylist if you don’t know how many items you need
- you want to add/remove/reorder after initialization
- you want something more flexible
Summary: 7.2.8
- See methods you need to know
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.