1 Lists Example MultipleValue Data Structures Arrays Lists
- Slides: 20
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; – Array. List<> implements the List<> interface using an array. – © Calvin College, 2009
7 The Array. List Class Array. Lists store an array of typed objects. List<a. Type> a. List = new Array. List<a. Type>(); a. List size array 0 © Calvin College, 2009
8 Array. Lists: Adding Values (add) Array. Lists handle their own memory allocation. List<a. Type> a. List = new Array. List<a. Type>(); a. List. add(a. Type. Object); a. List size array [0] [1] … [m-1] 1 a. Type. Object © Calvin College, 2009
9 Array. Lists: Accessing Values (get) Array. Lists provide indexed access. List<a. Type> a. List = new Array. List<a. Type>(); a. List. add(a. Type. Object); System. out. println(a. List. get(array. Index)); a. List size array [0] [1] … [m-1] 1 a. Type. Object © Calvin College, 2009
10 Array. Lists: Memory Allocation Array. Lists allocate memory automatically. List<a. Type> a. List = new Array. List<a. Type>(); a. List. add(a. Type. Object); System. out. println(a. List. get(array. Index)); a. List. add(a 2 nd. Type. Object); . . . a. List. add(an. M+1 st. Type. Object); a. List size array [0] [1] … [m-1][m] … m+1 a. Type. Object a 2 nd. Type. Object an. M+1 st. Type. Object © Calvin College, 2009
11 Iteration 1 ● Analysis ● Design ● Implementation ● Test © Calvin College, 2009
12 Array & Lists Syntax String[] my. Countries = new String[2]; my. Countries[0] = "Honduras"; my. Countries[1] = "Panama"; System. out. println(my. Countries. Array. length); System. out. println(my. Countries. Array[0]); List<String> my. Countries. List = new Array. List<String>(); my. Countries. List. add("Honduras"); my. Countries. List. add("Panama"); System. out. println(my. Countries. List. size()); System. out. println(my. Countries. List. get(0)); © Calvin College, 2009
13 Iteration 2 ● Analysis ● Design ● Implementation ● Test © Calvin College, 2009
14 Array. Lists: As Parameters Array. Lists 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). get. Continent. Name(). equals. Ignore. Case(continent)) { result++; } } return result; } © Calvin College, 2009
15 Array. Lists: As Return values Array. Lists can be returned as return values. private List<Country> load. Countries() { List<Country> result = new Array. List<Country>(); result. add(new Country("Algeria", "Africa")); result. add(new Country("Angola", "Africa")); . . . return result; } © Calvin College, 2009
16 Array. List: Copying List<Country> original = new Array. List<Country>(); // add two country objects to original (c 1 & c 2)… List<Country> reference. Copy = original; original size array 2 reference. Copy c 1 c 2 List<Country> shallow. Copy = (List<Country>)original. clone(); shallow. Copy size array 2 © Calvin College, 2009
17 List<Country> deep. Copy = deep. Copy(original); deep. Copy size array 2 c 1 copy c 2 Copy public List<Country> deep. Copy(List<Country> original) { List<Country> result = new Array. List<Country>(); for (int i = 0; i < original. size(); i++) result. add(new Country(my. Countries. get(i). get. Name(), my. Countries. get(i). get. Continent. Name(), my. Countries. get(i). get. Image. Name())); return result; } © Calvin College, 2009
18 Array. List Equality Similar issues arise when checking arraylist equality: – an. Array. List. equals(another. Array. List) 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: Array. List<RType>> ID – Initializing 2 -D lists: new Array. List<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: © Calvin College, 2009
- Random access array
- Parallel arrays in data structure
- Biology homology
- Array of arrays c++
- Java array operations
- Veteork
- C++ parallel arrays
- Why do we need arrays?
- Dynamic arrays and amortized analysis
- Arreglos unidimensionales en java
- Arreglos bidimensionales en java
- Arrays mips
- Polynomial representation using arrays
- Arrays in arm assembly
- Global arrays in c
- Computer science arrays
- Searching and sorting arrays in c++
- Arrays visual basic
- Python parallel arrays
- How many arrays in 24
- Arrays in pascal examples