Lecture 23, CS 302-7 and 8, March 14, 2012

 

  1. Review
    1. ArrayLists

                                                               i.      Methods

                                                             ii.      Add(val)

                                                            iii.      Size()

                                                           iv.      Add(ix,val)

                                                             v.      Set(ix,val)

                                                           vi.      Remove(ix)

  1. Inventory.java
  2. Copying ArrayLists
    1. If you just set equal, this copies the reference, just like arrays
    2. Use constructor

                                                               i.      Given names:

1.      ArrayList<String> namesCopy=new ArrayList<String>(names);

    1. Piece by piece (tedious) – using for loop
  1. When would you use an array?  When would you use an ArrayList?
    1. Arrays – easier syntax, faster

                                                               i.      Use when you know how many values you need to store

                                                             ii.      Use if you are really concerned about speed

    1. ArrayLists – can grow and shrink automatically

                                                               i.      Use when you don’t know how many values you need to store.

                                                             ii.      Note – arraylists, no initializer list syntax (one other disadvantage…)

    1. Reminder – length vs length() vs size()
  1. ArrayLists of primitives using wrapper classes (reminder)
    1. ‘wrappers and auto boxing’
    2. wrappers – wrapper classes for primitive types
    3. auto-boxing

                                                               i.      this means that you can still store the primitive values in the array list – it just needs the type of the wrapper class.

                                                             ii.      Exampl of what it means to be boxed:

Double wrapper=29.95;

double x=wrapper;

  1. Passing ArrayLists
    1. Reminder – can pass objects as parameters

                                                               i.      Example – Random random in Program2

    1. Like Arrays, and every other type we’ve encountered so far, we can pass ArrayLists as parameters to methods, and return them from methods.

                                                               i.      Must specify Type Parameter in both cases in method declaration.

public static ArrayList<Integer> methodName(ArrayList<Integer> aList)

{

 

}