Transcript ArrayLists

The ArrayList Class in Java
Computer Science 3
Gerb
Reference:
Objective: Understand ArrayLists in Java and
how they implement the List ADT.
The ArrayList class
• The ArrayList class in Java implements the
List interface
• Uses an array as its data structure
• If you need more space
– Creates a new array
– Copies everything from the old array to the new
• Implements operations in the List interface
Creating an ArrayList
• Two constructors
• First constructor takes no parameters
ArrayList myList = new ArrayList();
– Picks an initial size for the array
• Second constructor takes an integer
ArrayList myList=new ArrayList(60);
– Uses the number passed to the constructor as the initial
size for the array
• DON’T BE CONFUSED: 60 is the size of the
storage array
– NOT the size of the list
– All ArrayLists are empty (size = 0) when created
Elements of the ArrayList class
• Elements in an ArrayList are all of type Object.
• Add, set, etc. expect arguments of type Object
– Every Java class is a subclass of Object
– Includes arrays, strings and user defined classes
– Does not include primitive types (int, double,
boolean).
Wrapper Classes
• Java provides “wrapper” classes that allow integers and
doubles to be passed as Objects.
– Called Integer, Boolean, and Double (note upper case)
– Can store these in ArrayList
Integer int1 = new Integer(5);
Double d1 = new Double(4.01);
myList.add(int1);
myList.add(d1);
• If you try to put an integer or other primitive type into an
ArrayList, Java automatically converts it into a wrapper.
MyList.add(4); //will be added as an Integer
• This conversion is called autoboxing
Adding an item to an ArrayList
• Use the add operation
myList.add(“The String”);
• Adds the item after the last item in the list
– E.g. if 3 items already in the list, it becomes the 4th
• Argument can be any class instance or array
• Note that add changes the size of the list
Changing an item in an ArrayList
• Use the set method to change an item in an
ArrayList
myList.set(3,”New String”);
• First number is the index
– Must be less than the size of the list. Throws an
exception otherwise
– Start numbering from zero. I.e. 2nd item is numbered 1,
third item numbered 2, etc.
• Second argument can be any class instance or
array
• Does not change the size of the list
Retrieving an item from an
ArrayList
• Use the get method to get an item from an
ArrayList
System.out.print(myList.get(3));
• Parameter is the index
– Like set, parameter must be less than the size of the
list.
– Like set, start numbering from zero.
– Returns an Object
Removing an item from an ArrayList
• Use the remove method to take an item out of an
ArrayList
myList.remove(3);
• Parameter is the index of the item to be removed
– Start numbering from zero,
– Must be less than list size.
• Index of all later items in the list are moved down one
– E.g. if you remove item #1, item #2, becomes the new item #1
– Item #3 becomes the new item #2, etc.
• Decreases the size of the list by one
• Returns the item removed
Use size to get the number of items
in the list
• Takes no parameters, returns an integer
System.out.print(myList.size());
• Integer equal to the number of add
operations minus the number of remove
operations
• Not to be confused with the size of the
underlying array
Summary
• ArrayList class offers operations on a list
–
–
–
–
–
add
set
get
remove
size
• Items are numbered from zero
• Items are stored in an array which is enlarged
when necessary.