

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
array list is a list of an array we use in java programming cnfjdkcjk jddjjskj
Typology: Lecture notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!
2 Exercise
! First display all words. ! Then display them with all plurals (ending in "s") capitalized. ! Then display them in reverse order. ! Then display them with all plural words removed.
! Why or why not?
4 Lists
! each element is accessible by a 0-based index ! a list has a size (number of elements that have been added) ! elements can be added to the front, back, or elsewhere ! in Java, a list can be represented as an ArrayList object
5 Idea of a list
! The default behavior is to add to the end of the list. [hello, ABC, goodbye, okay]
! Think of an "array list" as an automatically resizing array object. ! Internally, the list is implemented using an array and a size field.
7 Type Parameters (Generics)
! This is called a type parameter or a generic class. ! Allows the same ArrayList class to store lists of different types. ArrayList
8 Learning about classes
! The link to the API Specs is on the course web site.
10 ArrayList vs. array 2
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")) { ... } }
for (int i = 0; i < names.length; i++) { if (names[i].equals("Benson")) { ... } } if (list.contains("Benson")) { ... }
11 Exercise, revisited
! 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.
13 ArrayList as parameter
// Removes all plural words from the given list. public static void removePlural( ArrayList
14 ArrayList of primitives?
// illegal -- int cannot be a type parameter ArrayList
// creates a list of ints ArrayList
16 Exercise
! Prints the average of the numbers. ! Prints the highest and lowest number. ! Filters out all of the even numbers (ones divisible by 2).
17 Exercise solution (partial) ArrayList
19 Out-of-bounds
! Reading or writing any index outside this range will cause an IndexOutOfBoundsException. ArrayList
20 ArrayList "mystery" ArrayList
for (int i = 0; i < list.size(); i++) { list.remove(i); } System.out.println(list);