Chapter 13 Array Lists and Arrays
Download
Report
Transcript 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"));
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
2
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)
3
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){
}
4
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;
}
5
Methods from Homework
public void addCoins(Coin aCoin)
public String listTheContents()
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.
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.
6
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
7
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.
8
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);
9
Compare ArrayList and Array
Array List
Arrays
Definition
A sequence of objects
An array is a fixed length
sequence of values of the same
type
Object type
Could be different
Same 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];
update
add, remove, set
Assignment to individual element
method type
non-static
static
10
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
11