{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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);
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern