ArrayLists in Java Programming

undefined
BUILDING JAVA
PROGRAMS
CHAPTER 10.1
ARRAY LISTS
undefined
OBJECTIVES!
Use 
ArrayList<E>
 to construct and analyze lists of
arbitrary length.
undefined
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?
undefined
ARRAYLIST<E>
 
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
undefined
ARRAYLIST<E> EXAMPLE
 
ArrayList<String> lies = 
new 
ArrayList<String>();
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"
undefined
ARRAYLIST<E>
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<String> list = 
new
 ArrayList<String>();
list.add(
"another value"
);
int
 length = list.size();
String myElement = list.get(2);
undefined
ARRAYLIST<E>
 
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!"
);
undefined
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.
undefined
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
.
undefined
CODE CODE CODE!
Write a method called 
getLines
 
which reads lines from the user until
!go
  is entered.  It should return an 
ArrayList<String>
 of all the
lines other than 
!go
.
Write a method called 
averageLineLength
 
which takes an
ArrayList<String> 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<String> 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.
undefined
WHAT DID WE COVER TODAY?
Use 
ArrayList<E>
 to construct and analyze lists of
arbitrary length.
Slide Note
Embed
Share

Explore the power of ArrayLists in Java to manage lists of arbitrary length efficiently. Learn how to manipulate ArrayLists without the need to declare a size upfront, and discover useful methods for adding, removing, and searching elements in an ArrayList.

  • Java Programming
  • ArrayLists
  • Data Structures
  • Java Development

Uploaded on Sep 10, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. BUILDING JAVA PROGRAMS CHAPTER 10.1 ARRAY LISTS

  2. OBJECTIVES! Use ArrayList<E> to construct and analyze lists of arbitrary length. 2 2

  3. 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? 3 3

  4. ARRAYLIST<E> 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 4 4

  5. ARRAYLIST<E> EXAMPLE ArrayList<String> lies = new ArrayList<String>(); 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" 5 5

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

  7. ARRAYLIST<E> How would you delete the element at a particular index? list.remove(2); And last but not least what if you want to insert and element at a particular index? list.add(2, "sweeeeeet!"); 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! 7 7

  8. 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. 8 8

  9. 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. 9 9

  10. CODE CODE CODE! Write a method called getLines which reads lines from the user until !go is entered. It should return an ArrayList<String> of all the lines other than !go. Write a method called averageLineLength which takes an ArrayList<String> 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<String> 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. 10 10

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

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#