Download ArrayList

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ArrayList
Collections
How Do I Use an ArrayList?
• First, import it so it is available for use:
import java.util.ArrayList;
-or-
import java.util.*;
How Do I Use an ArrayList?
• Next declare and initialize!
ArrayList<Point> myPoints;
myPoints = new ArrayList<Point>();
//note: The initial capacity of an ArrayList is 10
• Recall the Java array style
Point[] myPoints;
myPoints = new Points[10];
How do I add items?
• Call the add method (adds the item to
the back of the ArrayList).
Point p = new Point(1,2);
myPoints.add(p);
myPoints.add(new Point(2,3));
How many items are in the ArrayList?
• For an ArrayList, we use
• myPoints.size()
• For an array, we use
• myPointsArr.length
Note: size() gives number of items, length
gives the capacity
How do I retrieve items?
• For an ArrayList
• Point p = myPoints.get(0);
• For an array,
• Point p = myPointArr[0];
Example (Traversal and set)
• Given an ArrayList called myNames filled with
names, replace all occurrences of “marc” with
“mark”.
for(int i = 0; i < myNames.size(); i++)
{
if (myNames.get(i).equals(“marc”))
myNames.set(i, “mark”);
}
Note: The set method replaces what is at i with
the new element and returns the old element
(which is ignored in this case).
Traversal with “for each” loop
for(String s: myNames)
{
if (myNames.get(i).equals(“marc”))
myNames.set(i, “mark”);
}
Can elements only be added at the back of
the ArrayList?
• No! You can add an element at any valid index.
• Consider the ArrayList named myAlphabet:
[“apple”, “bear”, “cake”, “dog”]
• The call
myAlphabet.add(2, “zebra”);
results in
[“apple”,“bear”,“zebra”,“cake”,“dog”]
Note: No need to shift things over by hand!
ArrayList does this for you!!!
Special note on add
• Given the following ArrayList named myAlphabet:
[“apple”, “bear”, “cake”, “dog”]
The following two calls would produce identical
results:
myAlphabet.add(“zebra”)
myAlphabet.add(4, “zebra”);
Note: myAlphabet.add(5, “zebra”) will
cause an IndexOutOfBoundsException.
Easy to remove as well!
• Remember the pain of removing with arrays (having to shift
elements to the left). ArrayList does this for you!
• Given
[“apple”,“bear”,“zebra”,“cake”,“dog”]
• myAlphabet.remove(2) results in
[“apple”, “bear”, “cake”, “dog”]
Summary of ArrayList methods
class java.util.ArrayList<E>
int size()
boolean add(E e)
void add(int i, E e)
E get(int i)
appends e to end of list,
returns true
inserts e at index of i (0 <= i
<= size) moving elements
right as necessary
returns element at index of i
E set(int i,E e)
replaces element at i with e,
returns former element at i
E remove(int i)
removes element at I moving
elements left as necessary.
Arrays vs. ArrayList
Arrays
ArrayList
Declaration/Initialization Point[] p;
p = new Point[5];
ArrayList<Point> p;
p=new
ArrayList<Point>();
Assignment (replace)
p[3]=aPoint;
p.set(3, aPoint);
Insertion
Lots of code to shift
element right
p.add(2, aPoint);
Deletion
Lots of code to shift
elements left
p.remove(2);
Number of items
p.length
p.size();
Access
p[4].getX();
p.get(4).getX();