Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types.

Similar presentations


Presentation on theme: "Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types."— Presentation transcript:

1 Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types

2 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 Structures2

3 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 Structures3

4 4 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

5 Java Programming: Program Design Including Data Structures5 class Vector (continued)  The class Vector is contained in the package java.util  Programs must include either:  import java.util.*;  import java.util.Vector;

6 Vector Declaration  Declare/initialize Vector students = new Vector ();  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 Structures6

7 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 Structures7

8 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 students = new Vector (1000); Java Programming: Program Design Including Data Structures8

9 Size and capacity  Get the current size and capacity: Vector students = new Vector (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 Structures9

10 10 Vector stringList = new Vector (); stringList.add("Spring"); stringList.add("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Adding Elements add and addElement have identical functionality

11 Java Programming: Program Design Including Data Structures11 stringList.get(0); // “Spring” stringList.get(3); // “Winter” stringList.get(4); // ArrayIndexOutOfBounds Exception Accessing Elements

12 Java Programming: Program Design Including Data Structures12 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

13 Java Programming: Program Design Including Data Structures13 Primitive Data Types and the class Vector (continued)  Creating a Vector of Integer objects Vector list = new Vector (); 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

14 Java Programming: Program Design Including Data Structures14 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

15 Java Programming: Program Design Including Data Structures15 Members of the class Vector

16 Java Programming: Program Design Including Data Structures16 Members of the class Vector (continued)

17 Java Programming: Program Design Including Data Structures17 Members of the class Vector (continued)

18 Java Programming: Program Design Including Data Structures18 Members of the class Vector (continued)

19 Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types (continued)

20 Exercise Java Programming: Program Design Including Data Structures20 Vector a = new Vector (); 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());

21 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 Structures21

22 Multi-dimensional Vectors Java Programming: Program Design Including Data Structures22 Vector > a = new Vector >(); for (int i = 0; i < 4; i++) { Vector tmp = new Vector (); 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));

23 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 Structures23

24 Java Programming: Program Design Including Data Structures24 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

25 Java Programming: Program Design Including Data Structures25 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

26 Java Programming: Program Design Including Data Structures26 Enumeration Types (continued)

27 Java Programming: Program Design Including Data Structures27 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

28 Java Programming: Program Design Including Data Structures28 Enumeration Types (continued)

29 Java Programming: Program Design Including Data Structures29 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!”); }

30 Enumeration Types  See this site for more discussion and examples: http://download.oracle.com/javase/1.5.0/docs/guide/l anguage/enums.html http://download.oracle.com/javase/1.5.0/docs/guide/l anguage/enums.html Java Programming: Program Design Including Data Structures30

31 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 Structures31

32 Java Programming: Program Design Including Data Structures32 class String (Revisited)

33 Java Programming: Program Design Including Data Structures33 class String (Revisited) (continued)

34 Java Programming: Program Design Including Data Structures34 class String (Revisited) (continued)

35 Java Programming: Program Design Including Data Structures35 class String (Revisited) (continued)


Download ppt "Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types."

Similar presentations


Ads by Google