Download presentation

Presentation is loading. Please wait.

Published byKolton Childress Modified over 2 years ago

1
Chapter 13 Array Lists and Arrays

2
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
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
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
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
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
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
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
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
10 Compare ArrayList and Array Array ListArrays DefinitionA sequence of objectsAn array is a fixed length sequence of values of the same type Object typeCould be differentSame type Construct ArrayList coins = new ArrayList(); double[] data = new double[10]; # of elements coins.size() increases, shrinks a.length fixed index[0, coins.size()-1][0, a.length-1] access/ retrieve Get and cast, Coin c = (Coin)coins.get(i); element type followed by index, i.e., Coin aCoin = pocket[i]; updateadd, remove, setAssignment to individual element method typenon-staticstatic

11
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

Similar presentations

Presentation is loading. Please wait....

OK

Copyright © 2002 Pearson Education, Inc. Slide 1.

Copyright © 2002 Pearson Education, Inc. Slide 1.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Computer lab rules elementary ppt on cells Ppt on right to education act Animal sounds for kids ppt on batteries Ppt on space complexity Ppt on working of human eye and defects of vision Ppt on pricing policy in economics Ppt on metallic and nonmetallic minerals Ppt on natural disasters free download Ppt on earthquake in japan Ppt on viruses and bacteria youtube