Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]

Similar presentations


Presentation on theme: "CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]"— Presentation transcript:

1 CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]

2 The ArrayList class  Available in the package java.util.  An ArrayList object stores a list of items. The ArrayList automatically manages its size. The list grows as more items are added, or shrinks as some items are removed.  Items stored in an ArrayList all have the same reference type (i.e. defined by a class). Primitive type values can’t be stored as such in an ArrayList -> must use a wrapper type. CSC 142 J(part 1) 2

3 Declaring an ArrayList object  A named collection of data items of the same reference type.  To declare an ArrayList of Strings, write ArrayList phrases;  To declare an ArrayList of integers, write ArrayList numbers; NOT ArrayList numbers;  Always use the <> notation (= generics).  At this point, no memory is reserved for the ArrayList CSC 142 J(part 1) 3

4 Initializing an ArrayList  To initialize an ArrayList (once declared) phrases = new ArrayList (); numbers = new ArrayList ();  Other constructors are available e.g. // Make a copy of an existing ArrayList copy = new ArrayList (phrases); // Or if you know the size you need daysOfTheWeek = new ArrayList (7); // But daysOfTheWeek is NOT limited // to 7 elements CSC 142 J(part 1) 4

5 CSC 142 J(part 1) 5 Manipulating ArrayList elements (1)  To add an element to an ArrayList ArrayList a = new ArrayList (); a.add(“Monday”); a.add(“Wednesday”);  To access an ArrayList element, use its index (also called subscript), e.g., String first = a.get(0); // Monday String second = a.get(1); // Wednesday Indices start at 0. If an ArrayList has 10 elements, the first element has index 0 and the last element has index 9. To insert in an ArrayList a.add(1,“Tuesday”); //Wednesday is now at index 2

6 CSC 142 J(part 1) 6 Manipulating ArrayList elements (2)  To remove an element from an ArrayList String s = a.remove(1); // element at index 1 is // removed automatically, elements at indices > 1 // are shifted down // Also: boolean success = a.remove(s); // removes s (= some object) if present. Returns true // if s has been removed, and false if not.  Other operations: indexOf, contains, set See java documentation

7 CSC 142 J(part 1) 7 ArrayList and loops (1)  Loop with an index e.g. if a is an ArrayList of Rectangle objects for(int i = 0; i < a.size(); i ++) { Rectangle r = a.get(i); // work with r... }  Loop with an iterator Iterator it = a.iterator(); // could also use a ListIterator while(it.hasNext()) { Rectangle r = it.next(); // work with r... } Number of elements in a

8 ArrayList and loops (2)  for each loop for(Rectangle r : a) { // work with r... }  Which one to use?  To read the elements in no special order, use a for each loop.  To iterate through the ArrayList in a special way (e.g. from last to first, every other element), use a loop with an index.  for each and iterator loops are read only. CSC 142 J(part 1) 8

9 CSC 142 J(part 1) 9 Memory view ArrayList a= new ArrayList (); a.add(“Mercury”); a.add(“Earth”); a.add(1,”Venus”); a 0 1 2 ArrayList String “Mercury” String “Venus” String “Earth”


Download ppt "CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]"

Similar presentations


Ads by Google