Tuesday, March 15, 2011

Week 3 Lecture - Part 1


Notes from Week 3 Lecture.
Subjects covered in lecture:
  • ArrayList
  • Wrapper Class
  • Principles for designing classes
  • Object Relations:
    • Associations between objects (uni-directional & bi-directional association)
    • Aggregation & Composition
    • Dependency

ArrayList
Overview
  • is a Class, ie create objects from it
  • dynamically add more objects
  • remove objects
  • declaration of an ArrayList object requires specification of the type of objects in the list
    Private ArrayList<Dog> myPets;
  • initiation of the ArrayList also requires special syntax
    myPets = new ArrayList<Dog>();
  • Initially, ArrayList is empty
  • The index will change is elements earlier in the list are removed (ArrayList is dynamic)
Line 1 Col 1Line 1 Col 2
ActionCodeComments
Adding elementsmyPets.add(dog321);Add dog321 to end of list
Adding elementsadd(index, value);
eg myPets.add(2,scotty99);
Add the given value at the given index and shifts all subsequent values right
Retrieving elementsmyPets.get(3);Retrieve dog object added fourth
Set elements myPets.set(2, dog55); Update 3rd element to be dog55
Remove elementsmyPets.remove(dog321);Removes dog321 and returns true if successful
Remove elementsmyPets.remove(1);Removes dog added 2nd and returns true if successful
Clear ArrayList myPets.clear(); Removes all elements from the list
Size of ArrayListSystem.out.print( myPets.size() );Prints the number of elements in a list
Check for elements myPets.contains(dog321); Returns true if element is in ArrayList
First index myPets.indexOf(dog99); Returns first index of given value, -1 if not found.
Last index myPets.lastIndexOf(dog99); Returns the index of the last occurrence of value, -1 if not found
Iterating through an ArrayList for (int i=0; i < myPets.size(); i++)
{
 myPets.get(i).bark(2);
 System.out.print(myPets.get(i);
}
Make each dog bark twice and print dog
Enhanced Loopfor(Dog d : myPets)
{
 d.bark(2);
 System.out.print(d);
}
for every dog D found in the ArrayList myPets

Comparison of Static Data structures vs Dynamic Data Structures:
Static Data StructuresDynamic Data Structures
eg a plain arrayeg a Java ArrayList
size is fixedsize grows and shrinks as required
easy to set up & manipulatemore complicated to manipulate
not always flexibleMore flexible
not always efficientOften faster for large amounts of data

The lecturer then went on to say that an ArrayList is actually an Array, and everytime you add/remove elements to the ArrayList, a new Array is created and the ArrayList points to that, so this may cause issues, but it's not something we're going to cover. Or something like that - I'm going to listen to the recording to double-check what he said. I'm not a fan of lectures going into details that aren't covered in the lecture material / study guide / textbook because I can't read it later!!

Wrapper Class
  • Sometimes a primitive type variable needs to behave as an object
    • eg converting to a different format
    • eg being passed as a parameter to a method expecting an object
  • Each primitive data type has a wrapper class:
    Integer, Short, Long, Float, Double, Character, Byte and Boolean
  • All are public final - cannot be extended
  • Each Wrapper class has 2 constructors (except for Character wrapper class):
    • Integer(5) returns an Integer object with value 5
    • Integer("45") returns an Integer object with value 45
  • Tedious to convert back-and-forth, which is where autoboxing and unboxing come into it
ArrayList elements must all be objects. int is not an object, hence cannot be inserted into an ArrayList.
Before JDK 5

ArrayList integerList = new ArrayList();
int number = 42;
integerList.add(new Integer(number));
...
Integer anInteger;
anInteger = (Integer)integerList.get(2);
int myNumber = anInteger.intValue();


After JDK 6

ArrayList <Integer> integerList;
integerList = new ArrayList<Integer>();
int number = 42;
integerList.add(number);   //int autoboxed into Integer object before adding to ArrayList
...
Integer anInteger;
anInteger = integerList.get(2);
int myNumber = anInteger;  //After getting Integer out of ArrayList, int is auto unboxed from Integer


Thoughts:
  • I can see how an ArrayList is useful - re: assignment, you may not have a fixed number of programmers in a team, etc.
  • Why would I use an Array when I could use an ArrayList? Apart from not having to worry about primitive type variables.
  • I'm not sure when I'll use autoboxing/unboxing - when would I need an ArrayList of primitive type variables? Nonetheless, it's useful to know in case I need this later.

1 comment:

  1. From what I understand, you need to wrap up any primitive data types into an object if you want to serialise your object to write it to a file, otherwise there is the potential to loose the value of your primitive type. Boxing and unboxing means you just change the type of your declared variable (eg from int to Integer) and you dont need to change the rest of your code as java does the rest :)

    ReplyDelete