CSC152 2005F, Class 32: Vectors (1) Admin: * Homework: Find (or rewrite) the two recursive forms of exponentiation. Extend to "count" the number of recursive calls. * More homework: Try to replicate your claim that "add doesn't work when the index is size()" * Walker observes class Friday, Monday, Tuesday. * Today's class ends early. Overview: * Lots of vector stuff * Comparatively analyzing algorithms Vectors * Why when I create a vector with new Vector(10) does it seem to have size 0? * Similarly, why does the documentation say "default of 10" new Vector() but size still returns 0 * The designers distinguish between the "size" of the vector (number of elements) and the "capacity" (the length of the underlying array) * Why make this choice? So that some expansions are "free" What about strange error message? Note: JOAVT.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. New for loop for (TYPE NAME : VECTOROROTHERCOLLECTION) { body } Meaning Set NAME to the first value in the collection Execute the body Set NAME to the second value in the collection Execute the body Set NAME to the third value in the collection Execute the body ... Set NAME to the penultimate value in the collection Execute the body Set NAME to the final value in the collection Execute the body Vector strings; ... for (String s : strings) { pen.println(s); } vs. for (int i = 0; i < strings.size(); i++) { String s = strings.get(i); pen.println(s); } Six similar methods to put values in Vectors * set(int index, E element) * add(E element) * add(int index, E element) * setElementAt(E element, int index) * insertElementAt(E element, int index) * addElement(E element) * Similarities and differences * addElement returns void; one-param add returns boolean (which is always true); two-param add returns void * one-param add and addElement add the new value *at the end of the vector*, increasing the size by one * insertElementAt will also increase the size of the vector * two-param add can increase the size of the vector AS LONG as index = size() * set, however, does not increase the size of the vector, even when index= size() * Why have four variations of add and two variations of set? * Different implementations? * Different people designed the interfaces that Vector implements * The different interfaces specify the same general ideas, but with different names