Set If youre concern with uniqueness of the elements use Set Lesson 7 3 6 We

Set if youre concern with uniqueness of the elements

This preview shows page 36 - 43 out of 43 pages.

Set If you’re concern with uniqueness of the elements, use Set . >> Lesson 7 3 6 We are going to use one of these classes
Image of page 36

Subscribe to view the full document.

Using Set import java.util.HashSet; public class DemoSet { public static void main(String[] args) { HashSet<String> setFruits = new HashSet<String>(); setFruits.add("rambutan"); setFruits.add("jackfruit"); setFruits.add("mangoesteen"); setFruits.add("jackfruit"); System.out.println(setFruits); } } >> Lesson 7 3 7 Will not be added
Image of page 37
TRAVERSING LIST AND SET Lesson 8 >> Lesson 8 3 8
Image of page 38

Subscribe to view the full document.

Traversing List and Set Traversing elements either in List or Set allows you to further manipulate the object You may invoke the methods in the respective object Usually we used for loop to traverse the collection. Or you can use something more sophisticated, Enumeration or Iterator . >> Lesson 8 3 9
Image of page 39
Traversing using for loop import java.util.Vector; public class DemoVector2 { public static void main(String[] args) { Vector<String> fruits = new Vector<String>(); fruits.add("rambutan"); fruits.add("jackfruit"); fruits.add("mangoesteen"); for (int index = 0; index < fruits.size(); index++){ System.out.println(fruits.get(index)); } } } >> Lesson 8 4 0
Image of page 40

Subscribe to view the full document.

Traversing using Simplified for loop import java.util.Vector; public class DemoVector2 { public static void main(String[] args) { Vector<String> fruits = new Vector<String>(); fruits.add("rambutan"); fruits.add("jackfruit"); fruits.add("mangoesteen"); for (String fruit:fruits){ System.out.println(fruit); } } } >> Lesson 8 4 1
Image of page 41
Traversing using Enumeration import java.util.Vector; import java.util.Enumeration; public class DemoVector2 { public static void main(String[] args) { Vector<String> fruits = new Vector<String>(); fruits.add("rambutan"); fruits.add("jackfruit"); fruits.add("mangoesteen"); Enumeration<String> enumFruit = fruits.elements(); while (enumFruit.hasMoreElements()) { System.out.println(enumFruit.nextElement()); } } } >> Lesson 8 4 2 You need to refer to Enumeration API.
Image of page 42

Subscribe to view the full document.

THAT’S WRAP FOR TODAY See You in the Next Class >> Lecture 7 4 3
Image of page 43

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes