03-arraylist2-oop - CSE 143 Lecture 3 More ArrayList...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 3 More ArrayList ; object-oriented programming reading: 10.1; 8.1 - 8.7 slides created by Marty Stepp http://www.cs.washington.edu/143/
Image of page 1

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

View Full Document Right Arrow Icon
2 Out-of-bounds • Legal indexes are between 0 and the list's size() - 1 . Reading or writing any index outside this range will cause an IndexOutOfBoundsException . ArrayList<String> names = new ArrayList<String>(); names.add("Marty"); names.add("Kevin"); names.add("Vicki"); names.add("Larry"); System.out.println(names.get(0)); // okay System.out.println(names.get(3)); // okay System.out.println(names.get(-1)); // exception names.add(9, "Aimee"); // exception index 0 1 2 3 value Marty Kevin Vicki Larry
Image of page 2
3 ArrayList "mystery" ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 1; i <= 10; i++) { list.add(10 * i); // [10, 20, 30, 40, ..., 100] } • What is the output of the following code? for (int i = 0; i < list.size(); i++) { list.remove(i); } System.out.println(list); • Answer: [20, 40, 60, 80, 100]
Image of page 3

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

View Full Document Right Arrow Icon
4 ArrayList "mystery" 2 ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 1; i <= 5; i++) { list.add(2 * i); // [2, 4, 6, 8, 10] } • What is the output of the following code? int size = list.size(); for (int i = 0; i < size; i++) { list.add(i, 42); // add 42 at index i } System.out.println(list); • Answer: [42, 42, 42, 42, 42, 2, 4, 6, 8, 10]
Image of page 4
5 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 5

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

View Full Document Right Arrow Icon
6 Exercise • Write a method addStars that accepts an array list of strings as a parameter and places a * after each element.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the 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