Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.

Similar presentations


Presentation on theme: "1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks."— Presentation transcript:

1 1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks as needed.  The ArrayList class is part of the java.util package of the Java standard class library.

2 2 ArrayLists contain Objects  Numbers are not objects.  Unlike an array, you can not have an ArrayList of int s or d ouble s or b oolean s or char s.

3 3 ArrayList  Each element in the sequence can be accessed separately.  We can explicitly overwrite an object at a specified position in the sequence, thus changing its value.  We can inspect the object at a specified location in the sequence.  We can add an object into a specified position of the sequence.  We can add an object to the end of the sequence.  We can remove an object from a specified location in the sequence.

4 4 ArrayList s for AP CS A Notice that the AP CS A Subset requires the knowledge that ArrayList implements List.

5 5 java.util.ArrayList for AP CS A  int size() // returns the number of elements in this list  boolean add(E x) // appends x to the end of list; returns true  E get(int index) // returns the element at the specified position in this list.  E set(int index, E x) // replaces the element at index with x // returns the element formerly at the specified position

6 6 class java.util.ArrayList for AP CS A  void add(int index, E x) // inserts x at position index, sliding elements // at position index and higher to the right // (adds 1 to their indices) and adjusts size  E remove(int index) // removes element from position index, sliding // subsequent elements to the left (subtracts 1 from their // indices) and adjusts size // returns the element at the specified position in this list.

7 7 An example: import java.util.ArrayList; public class ArrayListTest { public ArrayListTest { System.out.println("ArrayListTest"); ArrayList aList = new ArrayList (); aList.add("Dan"); aList.add("George"); aList.add("Mary"); System.out.println("aList contains:"); for(int x = 0; x < aList.size(); x++) { System.out.println(aList.get(x)); } } }

8 8 Using enhanced FOR loop: import java.util.ArrayList; public class ArrayList_01_ForEach { public static void main (String [] arg) { System.out.println("ArrayListTest"); ArrayList aList = new ArrayList (); aList.add(new String("Dan")); aList.add("George"); aList.add("Mary"); System.out.println("aList contains:"); for(String e:aList) { System.out.println(e); } } }

9 9 Other ArrayList methods // ArrayListTest2 ArrayList students = new ArrayList (); students.add("Mary" ); students.add("James"); students.add("Kevin"); students.add(1, "Tanya"); String temp = students.get(3); System.out.println(temp); students.remove(2); students.set(1, "John"); System.out.println(students.size());

10 10 What happens? ArrayList_02 ArrayList students = new ArrayList (); students.add("Mary"); students.add("James"); students.add("Kevin"); students.add(1, "Tanya"); String temp = students.get(3); System.out.println(temp); students.remove(2); students.set(1, "John"); System.out.println(students.size()); MaryJamesKevin MaryTanyaJamesKevin temp MaryTanyaKevin MaryJohnKevin

11 11 An ArrayList is a sequence of objects.  Array lists can hold any kind of object. For the generic ArrayList, you must include the type of object the ArrayList will hold. ArrayList athletes = new ArrayList (); ArrayList csci6 = new ArrayList (); ArrayList accounts = new ArrayList ();  The ArrayList method add adds an Object of the type specified in the array list declaration.  The ArrayList method get returns an Object of the type specified in the array list declaration.

12 12 What happens? import java.util.ArrayList; public class StringTest { public StringTest() { ArrayList stringList = new ArrayList (); stringList.add("Fran"); stringList.add("Marie"); stringList.add("Joe"); System.out.println(stringList); } }


Download ppt "1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks."

Similar presentations


Ads by Google