Lecture 12b - • Eg: enum Season {SPRING, SUMMER, FALL,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Array of Strings Can easily be created with an initializer list o Eg: String[] stooge = {“Larry”, “Curly”, “Moe”}; o This is equivalent to: String[] stooge = new String[3]; stooge[0] = “Larry”; stooge[1] = “Curly”; stooge[2] = “Moe”; Enhanced for Loop Suitable only when iterating through all elements of an array or Collection class General form: for (array_base_type var : array_name) statement; Eg: double[] myArray = new double[20]; . . . for (double element : myArray) element = 0.0; Enumerated Types Are used to create custom data types consisting of a list of named constants General form: o enum typeName (VALUE_1, VALUE_2, . .. VALUE_N);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: • Eg: enum Season {SPRING, SUMMER, FALL, WINTER}; By convention, named constants should be in caps • This custom data type can be used to declare variables o Eg: Season highSeason, lowSeason; Constants are assigned to these variables using the general form: var = type_Name.VALUE_X; Eg: highSeason = Season.SUMMER; • An enumerated type is actually a class o The values are objects of the class o Has useful methods such as toString() and valueOf() o Eg: implicit use of toString() System.out.println(highSeason); This prints out: SUMMER o Can be used in switch statements READING AND EXERCISES CHAPTER 6...
View Full Document

This note was uploaded on 11/04/2010 for the course CPSC 233 taught by Professor Li during the Fall '09 term at University of Calgary.

Page1 / 2

Lecture 12b - • Eg: enum Season {SPRING, SUMMER, FALL,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online