Presentation is loading. Please wait.

Presentation is loading. Please wait.

BUILDING JAVA PROGRAMS CHAPTER 10.1 ARRAY LISTS. 22 OBJECTIVES! Use ArrayList to construct and analyze lists of arbitrary length.

Similar presentations


Presentation on theme: "BUILDING JAVA PROGRAMS CHAPTER 10.1 ARRAY LISTS. 22 OBJECTIVES! Use ArrayList to construct and analyze lists of arbitrary length."— Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 10.1 ARRAY LISTS

2 22 OBJECTIVES! Use ArrayList to construct and analyze lists of arbitrary length.

3 33 IN YOUR NOTEBOOKS… 1. Last time we computed average word length, we had to first ask the user for an integer before we could start inputting words… why? What did we need the integer for? 2. Can you think of a way to get an arbitrarily long list of words from the user without first asking for an integer?

4 44 ARRAYLIST Similar to arrays, but you don’t need to declare size up front… just add/remove as much as you like! It overrides toString() in a useful way You can use the same looping techniques you’re already good at

5 55 ARRAYLIST EXAMPLE ArrayList lies = new ArrayList (); lies.add("Nobody took the cookies from the cookie jar!"); lies.add("Programmers have more fun."); lies.add("Vegemite tastes great!"); // but I'm pretty sure the 2nd one's true... lies.remove(1); // What does this do? for (String lie : lies) { System.out.println("Lie: " + lie); } // it’s our old friend the "for-each"

6 66 ARRAYLIST How do you add a new value to the end of the ArrayList ? How do you get the number of elements in an ArrayList ? What if you want to get the element at a particular index? ArrayList list = new ArrayList (); list.add("another value"); int length = list.size(); String myElement = list.get(2);

7 77 ARRAYLIST How would you delete the element at a particular index? And last but not least… what if you want to insert and element at a particular index? You can also clear the whole array list with list.clear() and change the value at a location with list.set(index, value) These can all be found on page 654 in your text! list.remove(2); list.add(2, "sweeeeeet!");

8 88 FINDING VALUES… There are some other helpful methods you can use for searching in an ArrayList : contains(), indexOf() and lastIndexOf(). These are like the similarly named methods on the String class.

9 99 ARRAYLIST AND FOR-EACH Chapter 10.1 also talks about using a for-each loop with an ArrayList. What was the thing you have to watch out for when using a for- each loop with an ArrayList ? You can use the for-each loop as long as you’re only “visiting” each value. You can not change the ArrayList (by adding or removing a value) while you’re in the loop. If you attempt to modify the list in a for-each loop, Java will throw a somewhat helpfully-named ConcurrentModificationException.

10 10 CODE CODE CODE! Write a method called getLines which reads lines from the user until !go is entered. It should return an ArrayList of all the lines other than !go. Write a method called averageLineLength which takes an ArrayList and then prints each line and then the average line length. Try using the “for-each” loop. Make a main method that uses getLines and averageLineLength. Write a method called removeDuplicates which takes an ArrayList and uses a nested for loops to remove any duplicate strings. Then make your main method print the average line length before and after removing duplicates.

11 11 WHAT DID WE COVER TODAY? Use ArrayList to construct and analyze lists of arbitrary length.


Download ppt "BUILDING JAVA PROGRAMS CHAPTER 10.1 ARRAY LISTS. 22 OBJECTIVES! Use ArrayList to construct and analyze lists of arbitrary length."

Similar presentations


Ads by Google