Lists Example Multi-Dimensional Lists Multiple-Value Data Structures:

advertisement
1
Lists
●
●
Example
Multiple-Value Data Structures:
–
Arrays
– Lists
●
Multi-Dimensional Lists
© Calvin College, 2009
2
Example: Analysis
●
●
We’d like to guessing
game that drills students
on the countries of the
world.
A sketch of a solution
achieving this goal is
shown here.
Some hint goes here
(maybe an image
and/or text)…
Your guess…
Give up
© Calvin College, 2009
3
Example: Design
●
Elements:
A GUI controller;
– A Game class that:
–
•
•
•
–
Represents a list of countries;
Randomly chooses a country
as the current answer;
Produces a sequence of hints.
Some hint goes here
(maybe an image
and/or text)…
Your guess…
Give up
A Country class that:
•
Represents stuff about
countries.
map from commons.wikimedia.org
© Calvin College, 2009
4
Iteration 0
●
Analysis
●
Design
●
Implementation
●
Test
© Calvin College, 2009
5
Limitations of Arrays
●
Our initial iteration assumes that:
–
we know how many countries there are;
– we’re happy with the low-level array methods.
●
Moving beyond these assumptions forces
us to give up arrays because arrays:
–
are fixed in size at compile time;
– have a limited set of predefined methods.
© Calvin College, 2009
6
Lists
●
Java’s List data structure is more flexible:
–
Lists can grow or shrink at run time.
– Lists provide more predefined behaviors.
●
The Java Collections framework provides
classes supporting groups of objects:
List<> specifies an interface for an ordered
collection of typed objects;
– ArrayList<> implements the List<>
interface using an array.
–
© Calvin College, 2009
7
The ArrayList Class
ArrayLists store an array of typed objects.
List<aType> aList = new ArrayList<aType>();
aList
size
array
0
© Calvin College, 2009
8
ArrayLists: Adding Values (add)
ArrayLists handle their own memory allocation.
List<aType> aList = new ArrayList<aType>();
aList.add(aTypeObject);
aList
size
array
[0] [1] … [m-1]
1
aTypeObject
© Calvin College, 2009
9
ArrayLists: Accessing Values (get)
ArrayLists provide indexed access.
List<aType> aList = new ArrayList<aType>();
aList.add(aTypeObject);
System.out.println(aList.get(arrayIndex));
aList
size
array
[0] [1] … [m-1]
1
aTypeObject
© Calvin College, 2009
10
ArrayLists: Memory Allocation
ArrayLists allocate memory automatically.
List<aType> aList = new ArrayList<aType>();
aList.add(aTypeObject);
System.out.println(aList.get(arrayIndex));
aList.add(a2ndTypeObject);
...
aList.add(anM+1stTypeObject);
aList
size
array
[0] [1] … [m-1][m] …
m+1
aTypeObject
a2ndTypeObject
anM+1stTypeObject
© Calvin College, 2009
11
Iteration 1
●
Analysis
●
Design
●
Implementation
●
Test
© Calvin College, 2009
12
Array & Lists Syntax
String[] myCountries = new String[2];
myCountries[0] = "Honduras";
myCountries[1] = "Panama";
System.out.println(myCountriesArray.length);
System.out.println(myCountriesArray[0]);
List<String> myCountriesList = new ArrayList<String>();
myCountriesList.add("Honduras");
myCountriesList.add("Panama");
System.out.println(myCountriesList.size());
System.out.println(myCountriesList.get(0));
© Calvin College, 2009
13
Iteration 2
●
Analysis
CountryGuessController
●
Design
●
Implementation
●
Test
CountryGuessGame
+myAnswerIndex
+myHintCount
CountryGuessTest
+getHintText()
+guess()
+reset()
Country
1
*
+myName
+myContinent
© Calvin College, 2009
14
ArrayLists: As Parameters
ArrayLists can be passed as parameters.
private int count(List<Country> countries, String continent) {
int result = 0;
for (int i = 0; i < countries.size(); i++) {
if (countries.get(i).getContinentName()
.equalsIgnoreCase(continent)) {
result++;
}
}
return result;
}
© Calvin College, 2009
15
ArrayLists: As Return values
ArrayLists can be returned as return values.
private List<Country> loadCountries() {
List<Country> result = new ArrayList<Country>();
result.add(new Country("Algeria", "Africa"));
result.add(new Country("Angola", "Africa"));
...
return result;
}
© Calvin College, 2009
16
ArrayList: Copying
List<Country> original = new ArrayList<Country>();
// add two country objects to original (c1 & c2)…
List<Country> referenceCopy = original;
original
size
array
2
referenceCopy
c1
c2
List<Country> shallowCopy = (List<Country>)original.clone();
shallowCopy
size
array
2
© Calvin College, 2009
17
List<Country> deepCopy = deepCopy(original);
deepCopy
size
array
2
c1copy
c2Copy
public List<Country> deepCopy(List<Country> original) {
List<Country> result = new ArrayList<Country>();
for (int i = 0; i < original.size(); i++)
result.add(new Country(myCountries.get(i).getName(),
myCountries.get(i).getContinentName(),
myCountries.get(i).getImageName()));
return result;
}
© Calvin College, 2009
18
ArrayList Equality
Similar issues arise when checking arraylist
equality:
–
anArrayList.equals(anotherArrayList)
checks the two lists are the same size and that
their corresponding elements are equals().
–
This works for lists of strings, but special equality
checking routines must be written for lists of
other types.
– The String class has an equals() operator
that checks string equality properly.
© Calvin College, 2009
23
Multi-Dimensional Lists
●
Lists can also be multi-dimensional.
–
Declaring 2-D lists:
ArrayList<ArrayList<RType>> ID
–
Initializing 2-D lists:
new ArrayList<ArrayList<RType>>(rowsize)
–
Accessing 2-D array elements:
ID.get(row).get(column)
●
Multidimensional arrays are generally
easier to use and more efficient.
© Calvin College, 2009
24
Multi-dimensional List Structures
●
●
Multi-dimensional lists are useful for more
general multi-dimensional structures.
Example:
Text
+title: String
1
0..*
Paragraph
1
0..*
Sentence
Word
+value: String
0..*
1
© Calvin College, 2009
Download