This is called a type parameter or a generic class

This preview shows 10 out of 18 pages.

This is called a type parameter or a generic class. Allows the same ArrayList class to store lists of different types. ArrayList <String> names = new ArrayList <String> (); names.add("Marty Stepp"); names.add("Stuart Reges");
Image of page 10

Subscribe to view the full document.

11 Learning about classes • The Java API Specification is a huge web page containing documentation about every Java class and its methods. The link to the API Specs is on the course web site.
Image of page 11
12 ArrayList vs. array • construction String[] names = new String[5]; ArrayList<String> list = new ArrayList<String>(); storing a value names[0] = "Jessica"; list.add("Jessica"); retrieving a value String s = names[0]; String s = list.get(0);
Image of page 12

Subscribe to view the full document.

13 ArrayList vs. array 2 doing something to each value that starts with "B" for (int i = 0; i < names.length; i++) { if (names[i].startsWith("B")) { ... } } for (int i = 0; i < list.size(); i++) { if (list.get(i).startsWith("B")) { ... } } seeing whether the value "Benson" is found for (int i = 0; i < names.length; i++) { if (names[i].equals("Benson")) { ... } } if (list.contains("Benson")) { ... }
Image of page 13
14 Exercise, revisited • Write a program that reads a file and displays the words of that file as a list. First display all words. Then display them in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.
Image of page 14

Subscribe to view the full document.

15 Exercise solution (partial) ArrayList<String> allWords = new ArrayList<String>(); Scanner input = new Scanner(new File("words.txt")); while (input.hasNext()) { String word = input.next(); allWords.add(word); } System.out.println(allWords); // remove all plural words for (int i = 0; i < allWords.size(); i++) { String word = allWords.get(i); if (word.endsWith("s")) { allWords.remove(i); i--; } }
Image of page 15
16 ArrayList as parameter public static void name ( ArrayList< Type > name ) { • Example: // Removes all plural words from the given list. public static void removePlural( ArrayList<String> list ) { for (int i = 0; i < list.size(); i++) { String str = list.get(i); if (str.endsWith("s")) { list.remove(i); i--; } } } • You can also return a list: public static ArrayList< Type > methodName ( params )
Image of page 16

Subscribe to view the full document.

17 ArrayList of primitives? • The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type.
Image of page 17
Image of page 18
You've reached the end of this preview.

{[ 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