Vectors Strings and Enumeration Data Types Chris Kiekintveld

![A Weakness of Arrays s Suppose we declare an array of “Student” objects: Student[] A Weakness of Arrays s Suppose we declare an array of “Student” objects: Student[]](https://slidetodoc.com/presentation_image_h/2338c838f32335ee976cb03e0682751d/image-2.jpg)
![Resizing an Array (the hard way) Student[] students = new Student[10]; // do some Resizing an Array (the hard way) Student[] students = new Student[10]; // do some](https://slidetodoc.com/presentation_image_h/2338c838f32335ee976cb03e0682751d/image-3.jpg)
































- Slides: 35
Vectors, Strings, and Enumeration Data Types Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms
A Weakness of Arrays s Suppose we declare an array of “Student” objects: Student[] students = new Student[10]; s What if a new student joins the class? s 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[] students 2 = new Student[11]; for(int i = 0; i < students. length; i++) { students 2[i] = students[i]; } student[10] = new Student(); Java Programming: Program Design Including Data Structures 3
class Vector s The class Vector can be used to implement a list, replacing a simple array s The size of a Vector object can grow/shrink during program execution s 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) s The class Vector is contained in the package java. util s Programs must include either: s import java. util. *; s import java. util. Vector; Java Programming: Program Design Including Data Structures 5
Vector Declaration s Declare/initialize Vector<Student> students = new Vector<Student>(); s The syntax <…> is used to declare the type of object that will be stored in the Vector s If you add a different type of object, an exception is thrown s Not strictly necessary, but highly recommended (compiler warning) Java Programming: Program Design Including Data Structures 6
Vector Size/Capacity s The size of a vector is the number of elements s The capacity of a vector is the maximum number of elements before memory is needed s If size exceeds capacity when adding an element, the capacity is automatically increased s Declares a larger storage array s Copies existing elements, if necessary s Growing the capacity is expensive! s By default, the capacity doubles each time Java Programming: Program Design Including Data Structures 7
Setting Initial Capacity s 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 s Get the current size and capacity: Vector<Student> students = new Vector<Student>(1000); students. size(); // returns 0 students. capacity(); // returns 1000 s Setting size and capacity: // adds null elements or deletes elements if necessary students. set. Size(10); // increases capacity if necessary students. ensure. Capacity(10000); Java Programming: Program Design Including Data Structures 9
Adding Elements Vector<String> string. List = new Vector<String>(); string. List. add("Spring"); string. List. add("Summer"); string. List. add. Element("Fall"); string. List. add. Element("Winter"); Java Programming: Program Design Including Data Structures add and add. Element have identical functionality 10
Accessing Elements string. List. get(0); string. List. get(3); string. List. get(4); // “Spring” // “Winter” // Array. Index. Out. Of. Bounds Exception Java Programming: Program Design Including Data Structures 11
Primitive Data Types and the class Vector s Every component of a Vector object is a reference s Primitive data types are not objects s Corresponding to each primitive data type, Java provides a wrapper class s 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) s 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 tmp 2 = list. get(0). int. Value() //without Java Programming: Program Design Including Data Structures 13
Vector and the foreach loop s Each Vector object is a collection of elements s You can use a foreach loop to process its elements s Exactly like using a foreach loop with an array s Syntax: for (type identifier : vector. Object) 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. index. Of(new Integer(10)); System. out. println(a. to. String()); a. clear(); System. out. println(a. is. Empty()); Java Programming: Program Design Including Data Structures 20
Multi-dimensional Vectors s Can we have a 2 d vector? Yes! s Just like a 2 d array, but notation is more cumbersome Java Programming: Program Design Including Data Structures 21
Multi-dimensional Vectors Vector<Integer>> a = new 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. Array. List s Java has another class called Array. List s This class is almost identical in function to Vector, and has most of the same methods s Array. List is typically faster s Array. List 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 s Enumeration or enum types s User-defined data types s User specifies the values of that data type s Defined using the key word enum s Syntax example: s enum Grades {A, B, C, D, F}; s The values are identifiers s Called enumeration or enum constants s Must be unique within an enum type Java Programming: Program Design Including Data Structures 24
Enumeration Types (continued) s Each enum type is a special type of class s Values are (special types of) objects of that class s Using an enum type Grades my. Grade; my. Grade = Grades. B; System. out. println (“my. Grade: ” + my. Grade); s Each enum constant has an ordinal value s 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) s Because each enum type is a class, it can contain s Constructors, (private) data members, and methods s enum type considerations s s s 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 x. Pos = 0; public int y. Pos = 0; public void move(Directions dir) { switch(dir) { case Directions. North: y. Pos++; break; case Directions. South: y. Pos--; break; case Directions. East: x. Pos++; break; case Directions. West: x. Pos--; break; case default: System. out. println(“Invalid direction!”); } } Java Programming: Program Design Including Data Structures 29
Enumeration Types s 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 s Strings are essentially arrays of characters s The string class provides many functions for manipulating strings s s 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



































