Summary: 7.8.0

  • we learned about ArrayLists
    • dynamic, resizable arrays
      • grow and shrink as needed
    • how to declare and create them
    • add, remove, set, get entries
    • first element is 0
    • You can only put objects in a list
      • Use wrapper classes (Double, Integer, etc.) to put primitives in ArrayLists
        • Done automatically; autoboxing and unboxing

Concept Summary: 7.8.1

  • Autoboxing
    • Automatically wrapping a primitive type in a wrapper class object. For instance if you try to add an int value to a list, it will automatically be converted to an Integer object.
  • Abstract Method
    • A method that only has a declaration and no method body (no code inside the method).
  • ArrayList
    • An ArrayList can hold many objects of the same type. It can grow or shrink as needed. You can add and remove items at any index.
  • Add
    • You can add an object to the end of a list using listName.add(obj). You can add an object at an index of a list using add(index,obj). This will first move any objects at that index or higher to the right one position to make room for the new object.
  • Declaration
    • To declare an ArrayList use ArrayList name, where Type is the class name for the type of objects in the list. If you leave off the it will default to Object.
  • Creation
    • To create an ArrayList use new ArrayList, where Type is the class name for the type of objects you want to store in the list. There are other classes that implement the List interface, but you only need to know the ArrayList class for the exam.
  • Get
    • To get an object at an index from a list use listName.get(index).
  • Index
    • You can access and set values in a list using an index. The first element in a list called list1 is at index 0 list1.get(0). The last element in a list is at the length minus one - list1[list1.size() - 1].
  • Remove
    • To remove the object at an index use ListName.remove(index). This will move all object past that index to the left one index
  • Set
    • To set the value at an index in a list use listName.set(index,obj).
  • Size
    • Use listName.size() to get the number of objects in the list.
  • Wrapper Class
    • Classes used to create objects that hold primitive type values like Integer for int, Double for double and Boolean for boolean.
  • Unboxing
    • Automatically converting a wrapper object like an Integer into a primitive type such as an int.

Common Mistakes: 7.8.3

  • forgetting that set replaces the item at an index
  • remove moves all items to the right of an index to the left
  • incrementing an index when looping a list after removing an item
  • using arrayList[0] instead of arrayList.get(0)
  • using arrayList.length instead of arrayList.size()