CS 201 OBJECT ORIENTED PROGRAMMING II LECTURE 16

CS 201 OBJECT ORIENTED PROGRAMMING II LECTURE 16 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1

Last lecture’s topics • • Binary Search of an array. Using Arrays as counters. Vectors. Multi-dimentional Arrays 2

This lecture’ s topics • Array. List 3

The Array. List Class Arrays have a fixed size once they have been instantiated. What if we don't know how many elements we will need? For example, if we are • reading values from a file • returning search results We could create a very large array, but then we waste space for all unused elements. A better idea is to use an Array. List, which stores elements of object references and automatically expands its size, as needed. 4

The Array. List Class • The Array. List class is in the package: java. util • All Array. List elements are object references, so we could have an Array. List of Auto objects, Book objects, Strings, etc. • To store primitive types in an Array. List, use the wrapper classes (Integer, Double, Character, Boolean, etc. ) • The Array. List is a generic class. The Array. List class has been written so that it can store object references of any type specified by the client. 5

Declaring an Array. List • Use this syntax: Array. List<E> array. List. Name; E is a class name that specifies the type of object references that will be stored in the Array. List. • Example: Array. List<String> list. Of. Strings; Array. List<Auto> list. Of. Cars; Array. List<Integer> list. Of. Ints; 6

Array. List Constructors Constructor name and argument list Array. List<E> constructs an Array. List object of type E with an initial capacity of 10 Array. List<E>( int initial. Capacity ) constructs an Array. List object of type E with the specified initial capacity • The capacity of an Array. List is the total number of elements allocated to the list. • The size of an an Array. List is the number of elements that are used.

Instantiating an Array. List This list has a capacity of 10 Astronaut references (default capacity), but a size of 0. Array. List<Astronaut> list. Of. Astronauts = new Array. List<Astronaut>( ); This list has a capacity of 5 Strings, but a size of 0. Array. List<String> list. Of. Strings = new Array. List<String>( 5 ); 8

Array. List Methods Return value Method name and argument list boolean add( E element ) appends element to the end of the list void clear( ) removes all the elements in the list int size( ) returns the number of elements in the list E remove( int index ) removes the element at the specified index position

More Array. List Methods Return value Method name and argument list E get( int index ) returns the element at the specified index position; the element is not removed from the list. E set( int index, E element ) replaces the element at the specified index position with the specified element void trim. To. Size( ) sets the capacity of the list to its current size

Processing Array Lists Using a standard for loop: Class. Name current. Object; for ( int i = 0; i < array. List. Name. size( ); i++ ) { current. Object = array. List. Name. get( i ); // process current. Object } Example: Auto current. Auto; for ( int i = 0; i < list. Of. Autos. size( ); i++ ) { current. Auto = list. Of. Autos. get( i ); // process current. Auto } 11

Processing Array Lists • Example of a method definition that returns an Array. List: public Array. List<data_type> my. Method() • Example of setting an object into the list: Array. List<data_type> list=new Array. List<data_type>(); list. set(0, obj); //sets at index 0 the object obj 12

The Enhanced for Loop • Simplifies processing of lists • The standard form is: for ( Class. Name current. Object : array. List. Name ) { // process current. Object } • This enhanced for loop prints all elements of an Array. List of Strings named list: for ( String s : list ) { System. out. println( s ); } 13

Study Guide • Chapter 9 – Sections 9. 7 14
- Slides: 14