Upload
paula-newton
View
220
Download
5
Tags:
Embed Size (px)
Citation preview
© Calvin College, 2009
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.
Your guess… Give up
Some hint goes here (maybe an image and/or text)…
© 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.
– A Country class that:• Represents stuff about
countries. map from commons.wikimedia.org
Your guess… Give up
Some hint goes here (maybe an image and/or text)…
© 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
1 [0] [1] … [m-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
1 [0] [1] … [m-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
m+1
aTypeObject
[0] [1] … [m-1][m] …
a2ndTypeObject anM+1stTypeObject
© Calvin College, 2009
11
Iteration 1
● Analysis
● Design
● Implementation
● Test
© Calvin College, 2009
12
Array & Lists SyntaxString[] 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
● Design
● Implementation
● Test
CountryGuessController CountryGuessGame
+myAnswerIndex+myHintCount
+getHintText()+guess()+reset()
Country
+myName+myContinent
CountryGuessTest
*1
© Calvin College, 2009
14
ArrayLists can be passed as parameters.
ArrayLists: 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 can be returned as return values.
ArrayLists: 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: CopyingList<Country> original = new ArrayList<Country>();// add two country objects to original (c1 & c2)…List<Country> referenceCopy = original;
List<Country> shallowCopy = (List<Country>)original.clone();
original size array
2
c1 c2
referenceCopy
shallowCopy size array
2
© Calvin College, 2009
17
List<Country> deepCopy = deepCopy(original);
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;}
deepCopy size array
2
c1copy c2Copy
© 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
Multi-dimensional List Structures
● Multi-dimensional lists are useful for more general multi-dimensional structures.
● Example:
24
Text
+title: String Paragraph
Sentence
Word
+value: String
0..*1
0..*
1
0..*1