Announcements Review Announcements Lab 9 Inheritance Defining using

Announcements & Review Announcements Lab 9: Inheritance Defining & using class hiearchies Read: R&S 10 & 11 C&D Ch 8 Last Time: Abstract classes - enforces inclusion of some methods without a default implementation Interfaces - consistency of usage across many classes and methods - e. g. , colorable Today: back to arrays Collections - making arrays easier to use and implement Lecture 29: Array. List
![Basic Array Usage String[] apple. Names = {"Mc. Intosh", "Golden Delicious", "Granny Smith"}; for Basic Array Usage String[] apple. Names = {"Mc. Intosh", "Golden Delicious", "Granny Smith"}; for](http://slidetodoc.com/presentation_image/e9b5105c70ceb4393423a10410488009/image-2.jpg)
Basic Array Usage String[] apple. Names = {"Mc. Intosh", "Golden Delicious", "Granny Smith"}; for (int i = 0; i < apple. Names. length; i++) { System. out. println(apple. Names[i]); } // apple. Names -> 0 1 2 G r a n n y G o l d e n M c I n t S m i t h D e l i c i o u s o Lecture 29: Array. List s h

Advantages of Arrays 1. Straight forwarded extension of any base or user defined class Rectangle[][] rects = new Rectangle[10][100]; . . 2. Access to any named element is constant time 3. Straight forward iteration over all values String[] apple. Names = new String[70]; . . apple. Names[44] = “Fiji”; // direct update in constant time for (int i = 0; i < apple. Names. length; i++) { System. out. println(apple. Names[i]); } Lecture 29: Array. List

Limitations of Arrays 1. You cannot change the size of an array once you create it, i. e. , String[] names = new String[70]; 2. To make a bigger array, first allocate a larger array, and then copy the first array to it String[] more. Names = new String[100]; for (int i = 0; i < names. length; i++) { more. Names[i] = names[i]; } // if needed you can: names = more. Names; Lecture 29: Array. List

Limitations of Arrays 1. You cannot compare arrays with “==“ for (int i = 0; (i < names. length) && (i < more. Names. length); i++) { if (!more. Names[i]. equals(names[i])) { return false; } } return true; 2. You have to iterate over the array to print it for (int i = 0; i < names. length; i++) { System. out. println(names[i]); } Lecture 29: Array. List

Collection Classes • Collections group objects and provide methods to add, delete, sort, check membership, etc. <<interface>> Collection <<interface>> Set Hash. Set <<interface>> List Tree. Set Array. List Lecture 29: Array. List Linked. List

Array. List • Retains constant time access advantage of arrays • Solves resizing problems Array version: String[] apple. Names = {"Mc. Intosh", "Golden Delicious", "Granny Smith"}; for (int i = 0; i < apple. Names. length; i++) { System. out. println(apple. Names[i]); } Array. List version: type in blue - implements polymorphism Array. List<String> apple. Names = new Array. List<String>(); apple. Names. add("Mc. Intosh”); apple. Names. add("Golden Delicious”); apple. Names. add("Granny Smith”); for (int i = 0; i < apple. Names. size(); i++) { System. out. println(apple. Names. get(i)); } Lecture 29: Array. List

Array. List Methods 1. 2. 3. 4. size() - returns number of elements add(value) - adds at end of array get(index) - returns value at index set(index, value) - stores value at index, replaces old value 5. add(index, value) - adds at index position, shifts other elements to the right 6. remove(index) - removes value at index position, shifts elements to the left Lecture 29: Array. List

Array. List Methods (cont. ) 7. clear() - removes all elements 8. contains(value) - returns true if value in the list, false otherwise 9. index. Of(value) - returns index of first occurrence of value, or -1 if not found 10. last. Index. Of(value) - returns index of last occurrence Lecture 29: Array. List

Blue. J Lecture 29: Array. List

Collection Classes • One last sublty: Array. List and other Collections require an Object • Java provides “boxed” base types which turns a base type into an object • Example: int i = 0; Integer box = new Integer(1); i 0 box ---> • Declarations 1 – int -- Integer double -- Double – char -- Character boolean -- Boolean Lecture 29: Array. List

Collection Class with base type Array. List<Integer> amounts = new Array. List<Integer>(); Integer box = new Integer(1); // make an integer object amounts. add(box); // accepts an Integer object amounts. add(10); // accepts an int amounts. add(1000); // feature: automatic boxing of integer amounts. add(101); for (int i = 0; i < amounts. size(); i++) { System. out. println(amounts. get(i)); // feature: automatic unboxing of integer } Lecture 29: Array. List
- Slides: 12