Presentation is loading. Please wait.

Presentation is loading. Please wait.

ArrayList Collections.

Similar presentations


Presentation on theme: "ArrayList Collections."— Presentation transcript:

1 ArrayList Collections

2 How Do I Use an ArrayList?
First, import it so it is available for use: import java.util.ArrayList; -or- import java.util.*;

3 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];

4 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));

5 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

6 How do I retrieve items? For an ArrayList For an array,
Point p = myPoints.get(0); For an array, Point p = myPointArr[0];

7 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).

8 Traversal with “for each” loop
for(String s: myNames) { if (myNames.get(i).equals(“marc”)) myNames.set(i, “mark”); }

9 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!!!

10 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.

11 Easy to remove as well! [“apple”, “bear”, “cake”, “dog”]
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”]

12 Summary of ArrayList methods
class java.util.ArrayList<E> int size() boolean add(E e) appends e to end of list, returns true void add(int i, E e) inserts e at index of i (0 <= i <= size) moving elements right as necessary E get(int i) 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.

13 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();


Download ppt "ArrayList Collections."

Similar presentations


Ads by Google