chapter 10 2d arrays collection classes. topics arrays with more than one dimension java collections...

29
Chapter 10 2D Arrays Collection Classes

Post on 22-Dec-2015

236 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Chapter 10

2D Arrays

Collection Classes

Page 2: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Topics

• Arrays with more than one dimension

• Java Collections API

• ArrayList

• Map

Page 3: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Information Represented as a Table

Step -> Grade

0 1 2 3 4

0 10.50 12.00 14.50 16.75 18.00

1 20.50 22.25 24.00 26.25 28.00

2 34.00 36.50 38.00 40.35 43.00

3 50.00 60.00 70.00 80.00 99.99

Page 4: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Two-Dimensional Arrays

• In Java, data may be organized in a two-dimensional array. – A table is an example of a two-dimensional

array.

• In a two-dimensional array, two indices (in a table, one for the row and one for the column) are used to refer to the array element.

Page 5: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Two-Dimensional Arrays

• To declare our example array, we state:double[][] payScaleTable;

double payScaleTable[][];

• and create the array aspayScaleTable = new double[4][5];

Page 6: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Indexing Two-Dimensional Arrays

• To refer to an element at the second column (column 1) of the third row (row 2), we say

payScaleTable[2][1]

• Nested-for loops are useful for manipulating two-dimensional arrays.

Page 7: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Element Access for 2D Array

Page 8: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Two-Dimensional Arrays

• The concept of the two-dimensional array in Java is just that: a concept. There is no explicit structure called the “two-dimensional array” in Java.

• The two-dimensional array concept is implemented by using an array of arrays.

Page 9: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Two-Dimensional Arrays

• The sample array creationpayScaleTable = new double[4][5];

• is a shorthand forpayScaleTable = new double [4][ ];

payScaleTable[0] = new double [5];

payScaleTable[1] = new double [5];

payScaleTable[2] = new double [5];

Page 10: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Instanitating a 2D Array

Page 11: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Instantiation, continued

Page 12: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Size of Two-Dimensional Arrays

• The expressionpayScaleTable.length

• refers to the length of the payScaleTable array itself.

Page 13: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Size of Two-Dimensional Arrays

• The expression• payScaleTable[1].length

• refers to the length of the array stored at row 1 of payScaleTable.

Page 14: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Subarrays

• An array that is part of another array is called a subarray.

• An array of arrays may be initialized when it is created.

Page 15: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Ragged Arrays

• Subarrays may be different lengths.

• ExecutingtriangularArray = new double[4][ ];

for (int i = 0; i < 4; i++)

triangularArray[i] = new double [i + 1];

results in an array that looks like:

Page 16: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Lists and Maps

• The java.util library contains high-power classes for maintaining a collection of objects.

• These classes are collectively referred to as the Java Collection Framework (JCF).

Page 17: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Lists

• The List interface is one useful feature that can help us manage large numbers of objects.

• An interface defines the behavior of a class; a list of public methods without method bodies.

• We cannot create an instance of an interface.

Page 18: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Lists

• Two classes in the JCF implement the List interface:– ArrayList

– LinkedList

• The ArrayList class uses an array to manage data.

• The LinkedList class uses a technique called linked-node representation.

Page 19: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Creating a List

• To use the List interface, we declare the variable as List and assign an instance of the class that implements the List interface to it:

List myList;...myList = new ArrayList( );

• This approach permits a quick change of the implementation class if necessary.

Page 20: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Lists

• The default constructor will create an empty list with an initial capacity of 10.

• It is possible to increase the capacity of a list. However, it is better to create a list with the actual capacity we need, if we know in advance what that capacity is.

Page 21: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

List Methods

• The add method allows us to add objects to the list.

• The capacity method gives us the current capacity of a list.

• To find out the number of objects contained in a list, we use its size method.

Page 22: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

List Methods

• The remove method takes an element’s index as its parameter and allows us to remove an element from a list.

• The get method allows us to access objects stored in a list by giving their index position in the list.

• The iterator method also allows us to scan the elements inside a list or other JCF collection classes.

Page 23: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

List Iterators

• When we call a list’s iterator method, an Iterator object (an instance of a class that implements the Iterator interface) that supports two methods, hasNext and next, is returned.– hasNext returns true if the iterator has more

elements to access.– next returns the next element in the list

• Calling next if there are no more elements to access will result in an error.

Page 24: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Lists

• The iterator method is supported by many other java.util classes.

• A list cannot include primitive data values as its elements, but wrapper classes can adapt the values to acceptable list objects.

Page 25: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Maps

• Two classes implement the Map interface: – HashMap

– TreeMap

• TreeMap implements a subinterface of Map called SortedMap, where the entries in the map are sorted.

Page 26: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Maps

• A map consists of entries. Each entry is divided into two parts:– key– value

• Duplicate keys are not allowed in the map.

• Both the key and the value may be instances of any class.

Page 27: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Maps

• A map may be characterized as an expandable array with instances of any class as its indices.

• The main advantage of a map is its performance in locating an entry, given the key.

Page 28: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Maps

• We create an instance of a map:Map table;

...

table = new TreeMap();

• We add the key-value pairs to the map:table.put(“CS0101”, “Great course. Take

it.”);

– The first argument is the key. – The second argument is the value.

Page 29: Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map

Map Methods

• To remove an entry from a map, we use its remove method and pass the key as the argument.

• To retrieve all entries in the map, we use the entrySet method.

• To access all entries in a map, we use the entrySet method to obtain a set of entries, then use the iterator method as in the ArrayList example.

• The getKey and getValue methods are two particularly useful methods in the interface.

• These method retrieve the map entry’s key and value, respectively.