Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.

Similar presentations


Presentation on theme: "Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector."— Presentation transcript:

1 Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector object can grow/shrink during program execution You do not need to worry about the number of data elements in a vector

2 Java Programming: Program Design Including Data Structures 2 Members of the class Vector

3 Java Programming: Program Design Including Data Structures 3 Members of the class Vector (continued)

4 Java Programming: Program Design Including Data Structures 4 Members of the class Vector (continued)

5 Java Programming: Program Design Including Data Structures5 Members of the class Vector (continued)

6 Java Programming: Program Design Including Data Structures 6 Vectors (continued) Every element of a Vector object is a reference variable of the type Object To add an element into a Vector object: – Create appropriate object – Store data into object – Store address of object holding data into Vector object element

7 Declaring a vector object import java.util.*; or import java.util.Vector; Vector Vector stringList = new Vector (); Every component is an object Must use the wrapper classes Integer, Double, etc Vector list = new Vector ();

8 Java Programming: Program Design Including Data Structures8 Vector stringList = new Vector (); stringList.addElement("Spring"); stringList.addElement("Summer"); stringList.addElement("Fall"); stringList.addElement("Winter"); Vectors (continued)

9 Arrays of Objects Remember: int arr[] = new int[100]; Same with Objects

10 Java Programming: Program Design Including Data Structures 10 Additional String Methods

11 Java Programming: Program Design Including Data Structures11 Additional String Methods (continued)

12 Java Programming: Program Design Including Data Structures12 Additional String Methods (continued)

13 Java Programming: Program Design Including Data Structures 13 Additional String Methods (continued)

14 Arrays of String Objects String[] nameList = new String[10]; Declares and instantiates an array of 10 reference variables nameList[0] = “Sue Jones”; nameList[1] = “Tom Smith”; Etc. Creates String object, Allocates space

15 Arrays of String Objects output all names for (int index = 0; index < namesList.length;index++) System.out.println(namesList[index] + “ “); Access objects in the list if (nameList[0].equals (“Ray Rodgers”))

16 Arrays of Objects of Other Classes Clock[] arrivalTimeEmp[] = new Clock[100]; Declares and instantiates an array of 100 reference variables Instantiate Clock objects for (int ix = 0; ix < arrivalTimeEmp.length;ix++) arrivalTimeEmp [ix] = new Clock();//calls constructor Access objects in the list arrivalTimeEmp [40].setTime(7,30,5); for (int ix = 0; ix < arrivalTimeEmp.length;ix++) System.out.println(arrivalTimeEmp [ix] );

17 Arrays and Variable Length Parameter List To declare a variable length parameter list datatype … identifier dataType – primitive type or Java class Identifier is a variable length formal parameter A method can have both a variable length formal parameter and other formal parameters public static void myMethod (String name, double num, int … intList) A method can have at most one variable length formal parameter Variable length formal parameter must be last in the formal parameter list

18 example of variable length parameter list //method with formal argument public static double largest(double … numList) { double max; int ix; if (numList.length != 0) { max = list[0]; for (ix = 1; ix < numList.length;ix++) { if (max < numList[ix]) max = numList[ix]; } return max; } return 0.0; } //examples of actual arguments num1 = largest(34,56); num2 = largest(12.3,17.8, 56.8); num3 = largest(1.0,2.34,55,67.8); double [] list = {18.3, 56.7, 88.2, 37.8, 99.1, 26.4, 14.9}; num4 = largest(list);

19 Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other languages), these were often stored as constants. For example, in Java... public static final int SUIT_CLUBS = 0; public static final int SUIT_DIAMONDS = 1; public static final int SUIT_HEARTS = 2; public static final int SUIT_SPADES = 3;

20 Issues with this Approach There are, however, a number of issues with this approach. –Acceptable values are not obvious –No type safety –No name-spacing –Not printable

21 enum An enum type is a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week. Because they are constants, the names of an enum type's fields are in uppercase letters. Use the enum keyword. For example, you would specify a days-of-the-week enum type as: public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }

22 enum Grades {A, B, C, D, F}; enum Status {Freshman, Sophomore, Junior, Senior} enum LabColor {Chocolate, Yellow, Black}; Grades myGrade; myGrade = Grade.B; Can be used with switch


Download ppt "Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector."

Similar presentations


Ads by Google