120 likes | 387 Views
Chapter 13 Array Lists and Arrays. ArrayList Basics. Definition: An array list is a sequence of objects Construct an array List object, i.e. in the Purse.java ArrayList coins = new ArrayList(); Store variable number of objects coins.add(new Coin(0.1, "dime"));
E N D
ArrayList Basics • Definition: An array list is a sequence of objects • Construct an array List object, i.e. in the Purse.java ArrayList coins = new ArrayList(); • Store variable number of objects coins.add(new Coin(0.1, "dime")); • Retrieving Array List Elements: get and cast Coin c = (Coin) coins.get(i); Note: if int n = coins.size(); c = (Coin)coins.get(n); // ERROR reason:index values are 0...n-1, bounds errors
Several Methods for ArrayList Java API • coins.add(Object o) • coins.set(int index, Object o) • coins.get(int index) • coins.add(int index, Object o) • coins.set(int index, Object o) • Coins.remove(int index)
Simple Array List Algorithms • Searching a value public boolean search(Coin aCoin) { } • Counting # of coins that match given aCoin public int count(Coin aCoin){ } • Finding the Maximum public Coin getMaximum{ } • Adding Coins public void addCoins(Coin coinType, int count){ }
Stepping Through all Elements • for (int i = 0; i < coins.size(); i++){ Coin c = (Coin)coins.get(i);do something with c} • Public int count(Coin aCoin){ int matches = 0; for (int i = 0; i < coins.size(); i++){ Coin c = (Coin)coins.get(i); if(c.equals(aCoin)) matches++; //found a match } return matches; }
Methods from Homework • public void addCoins(Coin aCoin) • inserts aCoin in its proper place in the array list coins. • assume that coins is already sorted in the increasing order of the value of the coins. • public String listTheContents() • returns a string of the values of the coins of the purse. • public void remove( Coin aCoin, int count) • removes count occurrences of aCoin from the array list coins. • If coins does not have count occurrences of aCoin, the method throws an IllegalArgumentException.
Storing Numbers in Array List • Array lists store objects • Use wrapper classes to store numbers Double wrapper = new Double(29.95);double unwrapped = wrapper.doubleValue() • ArrayList data = new ArrayList();data.add(wrapper);unwrapped = ((Double).data.get(i)).doubleValue(); • Also have Integer and Boolean wrappers
Arrays • Definition: An array is a fixed length sequence of values of the same type. • How do you declare arrays? double[] data = new double[10]; • How do you work with arrays? by specifying the subscript, i.e., coin[] pocket = {dime, quarter, dollar, nickel}; Coin aCoin = pocket[2]; //aCoin is dollar • length field stores the number of elements in an array.
Copying Arrays • Copy & Clone • System.arrayCopy(fromArray, fromIndex, toArray, toIndex, size) • When will you use array copy when you want to add/remove an item Insert: System.arrayCopy(a, i, a, i+1, a.length-i-1); a[i] = x; Remove: System.arrayCopy(a, i+1, a, i, a.length-i-1);
Problem Statement for DataSet • Partially filled arrays • Companion variable to tell how many elements in the array are actually used. • data.size() is the capacity of the array data, while dataSize is the current size of the array. • Two java files • DataSet.java • DataSetTest.java