Docsity

Lecture Slides on Array Lists - Introduction to Programming II | CS 112, Study notes of Computer Science

Toggle sidebar
bg1
Intro to Programming II
ArrayLists
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science — University of San Francisco – p. 1/??
8-2: The ArrayList class
•Last time, we learned about how to use arrays in Java.
•Java also provides an ArrayList class that can help manage
arrays.
•ArrayList is a generic container.
◦That means that we can use the same container to store
different kinds of elements.
Departmentof Computer Science — University of San Francisco – p. 2/??
8-3: The ArrayList class
•An example:
ArrayList band = new ArrayList(3);
band.add(‘‘john’’);
band.add(‘‘paul’’);
band.add(‘‘george’’);
band.add(‘‘ringo’’);
Departmentof Computer Science — University of San Francisco
8-4: The ArrayList class
•Notice that we declared an initial array size.
•The array is then able to grow dynamically beyond that.
◦It’s better to allocate in advance if we can.
•We can also remove elements, and access them.
•We can also add in the middle of a list: band.add(1,
‘‘ringo’’);
Departmentof Computer Science — University of San Francisco – p. 4/??
8-5: Comparison with Arrays
•With arrays, we’d need to allocate a new arrayand copy
everything over. (yuck!)
int []array1 = new int[5];
for (i = 0; i < 5; i++) {
array1 = i;
}
int []array2= new int[10];
for (i = 0; i < 5; i++) {
array2[i] = array1[i];
}
array1 = array2;
Departmentof Computer Science — University of San Francisco – p. 5/??
8-6: Accessing elements
•get(index) lets us access the element at a particular index.
•Elements in an ArrayList are stored as Objects.
•This means that we need to cast them back to Strings.
•Can’t store primitives.
String name = (String)band.get(2);
Departmentof Computer Science — University of San Francisco
pf3

Partial preview of the text

Download Lecture Slides on Array Lists - Introduction to Programming II | CS 112 and more Study notes Computer Science in PDF only on Docsity!

Intro to Programming II^ ArrayLists^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p. 1/

??

8-2:^ The ArrayList class^ •^ Last time, we learned about how to use arrays in Java.^ •^ Java also provides an ArrayList class that can help managearrays.^ •^ ArrayList is a^ generic

container. ◦^ That means that we can use the same container to storedifferent kinds of elements.

Department of Computer Science — University of San Francisco – p. 2/

??

8-3:^ The ArrayList class^ •^ An example:^ ArrayList^ band^ =

new^ ArrayList(3);band.add(‘‘john’’);band.add(‘‘paul’’);band.add(‘‘george’’);band.add(‘‘ringo’’);

Department of Computer Science — University of San Fra

8-4:^ The ArrayList class^ •^ Notice that we declared an initial array size.^ •^ The array is then able to grow

dynamically^ beyond that. ◦^ It’s better to allocate in advance if we can. • We can also remove elements, and access them. • We can also add in the middle of a list:

band.add(1, ‘‘ringo’’);

Department of Computer Science — University of San Francisco – p. 4/

??

8-5:^ Comparison with Arrays^ •^ With arrays, we’d need to allocate a new array and copyeverything over. (yuck!)^ int^ []array1^ =^ new

int[5];for (i = 0; i < 5; i++)^ { array1 = i; } int []array2= new int[10];for (i = 0; i < 5; i++)^ { array2[i] = array1[i]; } array1 = array2;

Department of Computer Science — University of San Francisco – p. 5/

??

8-6:^ Accessing elements^ •^ get(index) lets us access the element at a particular index.^ •^ Elements in an ArrayList are stored as

Objects.

-^ This means that we need to cast them back to Strings. •^ Can’t store primitives.^ String^ name^ = (String)band.get(2);

Department of Computer Science — University of San Fra

8-7:^ Exercise 1^ •^ Redo the Student exercise from Monday using an ArrayList.^ ◦^ Prompt the user for a number of students.^ ◦^ Store the students in an ArrayList.^ ◦^ Print them out in order.

Department of Computer Science — University of San Francisco – p. 7/

??

8-8:^ Iterators^ •^ It can be cumbersome to iterate through a list using integers.^ •^ What if the size of the list changes while we’re iterating over it?^ •^ Why do we need to know the size of the array to do somethingto each element?^ •^ Java provides an

abstraction^ for us known as an iterator.

Department of Computer Science — University of San Francisco – p. 8/

??

8-9:^ Example List^ myList^ = ArrayList(10);//^ fill^ in^ the^ listListIterator^ i^ =^ myList.ListIterator();while^ (i.hasNext())System.out.println(i.next());

Department of Computer Science — University of San Fra

8-10:^ Exercise 2^ •^ Modify the previous code to use a ListIterator

Department of Computer Science — University of San Francisco – p. 10/

??

8-11:^ Finding elements^ •^ We can use indexOf to find where an element is located.^ •^ remove lets us remove things.^ int^ index^ =^ band.indexOf(‘‘ringo’’)band.remove(index)

Department of Computer Science — University of San Francisco – p. 11/

??

8-12:^ Exercise 3^ •^ Add a method that allows the user to enter the name of astudent.^ •^ Loop through the array to find that student and remove them.^ •^ Now try doing it with indexOf

Department of Computer Science — University of San Fran