1 / 11

Chapter 13 Array Lists and Arrays

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"));

holland
Download Presentation

Chapter 13 Array Lists and Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13Array Lists and Arrays

  2. 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

  3. 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)

  4. 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){ }

  5. 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; }

  6. 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.

  7. 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

  8. 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.

  9. 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);

  10. Compare ArrayList and Array

  11. 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

More Related