Vectors, Strings, and Enumeration Data Types Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms A Weakness of Arrays Suppose we declare an array of “Student” objects: Student[] students = new Student[10]; What if a new student joins the class? The size of an array cannot be increased after it is instantiated Java Programming: Program Design Including Data Structures 2 Resizing an Array (the hard way) Student[] students = new Student[10]; // do some stuff… // now we need to add student 11 Student[] students2 = new Student[11]; for(int i = 0; i < students.length; i++) { students2[i] = students[i]; } student[10] = new Student(); Java Programming: Program Design Including Data Structures 3 class Vector The class Vector can be used to implement a list, replacing a simple array The size of a Vector object can grow/shrink during program execution The Vector will automatically grow to accommodate the number of elements you put in it Java Programming: Program Design Including Data Structures 4 class Vector (continued) The class Vector is contained in the package java.util Programs must include either: import java.util.*; import java.util.Vector; Java Programming: Program Design Including Data Structures 5 Vector Declaration Declare/initialize Vector<Student> students = new Vector<Student>(); The syntax <…> is used to declare the type of object that will be stored in the Vector If you add a different type of object, an exception is thrown Not strictly necessary, but highly recommended (compiler warning) Java Programming: Program Design Including Data Structures 6 Vector Size/Capacity The size of a vector is the number of elements The capacity of a vector is the maximum number of elements before more memory is needed If size exceeds capacity when adding an element, the capacity is automatically increased Declares a larger storage array Copies existing elements, if necessary Growing the capacity is expensive! By default, the capacity doubles each time Java Programming: Program Design Including Data Structures 7 Setting Initial Capacity If you know you will need a large vector, it may be faster to set the initial capacity to something large Vector<Student> students = new Vector<Student>(1000); Java Programming: Program Design Including Data Structures 8 Size and capacity Get the current size and capacity: Vector<Student> students = new Vector<Student>(1000); students.size(); // returns 0 students.capacity(); // returns 1000 Setting size and capacity: // adds null elements or deletes elements if necessary students.setSize(10); // increases capacity if necessary students.ensureCapacity(10000); Java Programming: Program Design Including Data Structures 9 Adding Elements Vector<String> stringList = new Vector<String>(); stringList.add("Spring"); stringList.add("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Java Programming: Program Design Including Data Structures add and addElement have identical functionality 10 Accessing Elements stringList.get(0); stringList.get(3); stringList.get(4); // “Spring” // “Winter” // ArrayIndexOutOfBounds Exception Java Programming: Program Design Including Data Structures 11 Primitive Data Types and the class Vector Every component of a Vector object is a reference Primitive data types are not objects Corresponding to each primitive data type, Java provides a wrapper class JDK 5.0 provides autoboxing and auto-unboxing of primitive data types Java Programming: Program Design Including Data Structures 12 Primitive Data Types and the class Vector (continued) Creating a Vector of Integer objects Vector<Integer> list = new Vector<Integer>(); list.add(13); // with autoboxing list.add(new Integer(25)); // without autoboxing int tmp = list.get(0); // with autounboxing int tmp2 = list.get(0).intValue() //without Java Programming: Program Design Including Data Structures 13 Vector and the foreach loop Each Vector object is a collection of elements You can use a foreach loop to process its elements Exactly like using a foreach loop with an array Syntax: for (type identifier : vectorObject) statements Java Programming: Program Design Including Data Structures 14 Members of the class Vector Java Programming: Program Design Including Data Structures 15 Members of the class Vector (continued) Java Programming: Program Design Including Data Structures 16 Members of the class Vector (continued) Java Programming: Program Design Including Data Structures 17 Members of the class Vector (continued) Java Programming: Program Design Including Data Structures 18 Vectors, Strings, and Enumeration Data Types (continued) Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Exercise Vector<Integer> a = new Vector<Integer>(); a.add(4); a.add(7); a.add(10); a.set(1, 5); int tmp = a.remove(0); System.out.println(tmp); System.out.println(a.indexOf(new Integer(10)); System.out.println(a.toString()); a.clear(); System.out.println(a.isEmpty()); Java Programming: Program Design Including Data Structures 20 Multi-dimensional Vectors Can we have a 2d vector? Yes! Just like a 2d array, but notation is more cumbersome Java Programming: Program Design Including Data Structures 21 Multi-dimensional Vectors Vector<Vector<Integer>> a = new Vector<Vector<Integer>>(); for (int i = 0; i < 4; i++) { Vector<Integer> tmp = new Vector<Integer>(); for (int j = 0; j < 4; j++) { tmp.add(i+j); } a.add(tmp); } System.out.println(a.get(2).get(3)); System.out.println(a.get(0).get(2)); Java Programming: Program Design Including Data Structures 22 Vector vs. ArrayList Java has another class called ArrayList This class is almost identical in function to Vector, and has most of the same methods ArrayList is typically faster ArrayList should *not* be used if your code is multi-threaded (i.e., if you allow parallel execution) Java Programming: Program Design Including Data Structures 23 Enumeration Types Enumeration or enum types User-defined data types User specifies the values of that data type Defined using the key word enum Syntax example: enum Grades {A, B, C, D, F}; The values are identifiers Called enumeration or enum constants Must be unique within an enum type Java Programming: Program Design Including Data Structures 24 Enumeration Types (continued) Each enum type is a special type of class Values are (special types of) objects of that class Using an enum type Grades myGrade; myGrade = Grades.B; System.out.println (“myGrade: ” + myGrade); Each enum constant has an ordinal value Ordinal value of the first enum constant is 0 Java Programming: Program Design Including Data Structures 25 Enumeration Types (continued) Java Programming: Program Design Including Data Structures 26 Enumeration Types (continued) Because each enum type is a class, it can contain Constructors, (private) data members, and methods enum type considerations Defined using enum rather than class enum types are implicitly final enum constants are implicitly static You cannot instantiate objects using the operator new Constructors are implicitly private You cannot create new classes from an enum type Java Programming: Program Design Including Data Structures 27 Enumeration Types (continued) Java Programming: Program Design Including Data Structures 28 public enum Directions{North, South, East, West}; public int xPos = 0; public int yPos = 0; public void move(Directions dir) { switch(dir) { case Directions.North: yPos++; break; case Directions.South: yPos--; break; case Directions.East: xPos++; break; case Directions.West: xPos--; break; case default: System.out.println(“Invalid direction!”); } } Java Programming: Program Design Including Data Structures 29 Enumeration Types See this site for more discussion and examples: http://download.oracle.com/javase/1.5.0/docs/guide/l anguage/enums.html Java Programming: Program Design Including Data Structures 30 Strings Strings are essentially arrays of characters The string class provides many functions for manipulating strings Searching/matching operations Replacing characters Finding characters Trimming whitespace Etc. Java Programming: Program Design Including Data Structures 31 class String (Revisited) Java Programming: Program Design Including Data Structures 32 class String (Revisited) (continued) Java Programming: Program Design Including Data Structures 33 class String (Revisited) (continued) Java Programming: Program Design Including Data Structures 34 class String (Revisited) (continued) Java Programming: Program Design Including Data Structures 35