120 likes | 125 Views
Learn about the differences between arrays and ArrayList in Java, including their advantages, limitations, and methods. Understand how to use ArrayList to manipulate collections of objects.
E N D
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 Announcements & Review Lecture 29: ArrayList
Basic Array Usage String[] appleNames = {"McIntosh", "Golden Delicious", "Granny Smith"}; for (int i = 0; i < appleNames.length; i++) { System.out.println(appleNames[i]); } // appleNames -> Lecture 29: ArrayList
Advantages of Arrays • Straight forwarded extension of any base or user defined class Rectangle[][] rects = new Rectangle[10][100]; .... • Access to any named element is constant time String[] appleNames = new String[70]; .... appleNames[44] = “Fiji”; // direct update in constant time • Straight forward iteration over all values for (int i = 0; i < appleNames.length; i++) { System.out.println(appleNames[i]); } Lecture 29: ArrayList
Limitations of Arrays • You cannot change the size of an array once you create it, i.e., String[] names = new String[70]; • To make a bigger array, first allocate a larger array, and then copy the first array to it String[] moreNames = new String[100]; for (int i = 0; i < names.length; i++) { moreNames[i] = names[i]; } // if needed you can: names = moreNames; Lecture 29: ArrayList
Limitations of Arrays 1. You cannot compare arrays with “==“ for (int i = 0; (i < names.length) && (i < moreNames.length); i++) { if (!moreNames[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: ArrayList
Collection Classes • Collections group objects and provide methods to add, delete, sort, check membership, etc. <<interface>> Collection <<interface>> Set <<interface>> List HashSet TreeSet ArrayList LinkedList Lecture 29: ArrayList
ArrayList • Retains constant time access advantage of arrays • Solves resizing problems Array version: String[] appleNames = {"McIntosh", "Golden Delicious", "Granny Smith"}; for (int i = 0; i < appleNames.length; i++) { System.out.println(appleNames[i]); } ArrayList version: type in blue - implements polymorphism ArrayList<String> appleNames = new ArrayList<String>(); appleNames.add("McIntosh”); appleNames.add("Golden Delicious”); appleNames.add("Granny Smith”); for (int i = 0; i < appleNames.size(); i++) { System.out.println(appleNames.get(i)); } Lecture 29: ArrayList
ArrayList Methods • 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 • add(index, value) - adds at index position, shifts other elements to the right • remove(index) - removes value at index position, shifts elements to the left Lecture 29: ArrayList
ArrayList Methods (cont.) 7. clear() - removes all elements 8. contains(value) - returns true if value in the list, false otherwise 9. indexOf(value) - returns index of first occurrence of value, or -1 if not found 10. lastIndexOf(value) - returns index of last occurrence Lecture 29: ArrayList
BlueJ Lecture 29: ArrayList
Collection Classes • One last sublty: ArrayList 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 • Declarations • int -- Integer double -- Double • char -- Character boolean -- Boolean 0 box ---> 1 Lecture 29: ArrayList
Collection Class with base type ArrayList<Integer> amounts = new ArrayList<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: ArrayList