Docsity

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

Partial preview of the text

Download Array Lists - Introduction to Programming - Lecture Slides | 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-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

[]array

=^ new

int[5]; for^ (i^

=^ 0;^ i <

5;^ i++)

array^

=^ i;

} int^ []array2=

new^ int[10]; for^ (i^

=^ 0;^ i <

5;^ i++)

array2[i]

=^ array1[i];

} array^

=^ array2;

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

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-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-13:^ Other methods^ •^ also:^ ◦

size() - tells us how many elements are in the array. ◦ isEmpty() - tells us whether there’s anything in the list. ◦ contains(Object o) - tells us whether an object is in the list. ◦ clear() - removes everything.

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

8-14:^ Specifying element types^ •^ If we want to avoid casting elements, we can also define theArrayList to accept particular sorts of objects:^ ◦

Java 1.5 only ... ArrayList

al^ =^

new^ ArrayList(3);

al.add("john");al.add("paul");al.add("george");al.add("ringo");String^

name^ = al.get(2); System.out.println(name);

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

8-16:^ ArrayList vs arrays^ •^ ArrayLists manage inserting, searching, and removing for you.^ •^ Can grow dynamically.^ •^ Only work with objects, not primitives.^ •^ Provides some nice convenience methods.

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