Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map."— Presentation transcript:

1 Chapter 10 2D Arrays Collection Classes

2 Topics Arrays with more than one dimension Java Collections API ArrayList Map

3 Information Represented as a Table Step -> Grade 01234 010.5012.0014.5016.7518.00 120.5022.2524.0026.2528.00 234.0036.5038.0040.3543.00 350.0060.0070.0080.0099.99

4 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.

5 Two-Dimensional Arrays To declare our example array, we state: double[][] payScaleTable; double payScaleTable[][]; and create the array as payScaleTable = new double[4][5];

6 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.

7 Element Access for 2D Array

8 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.

9 Two-Dimensional Arrays The sample array creation payScaleTable = new double[4][5]; is a shorthand for payScaleTable = new double [4][ ]; payScaleTable[0] = new double [5]; payScaleTable[1] = new double [5]; payScaleTable[2] = new double [5]; …

10 Instanitating a 2D Array

11 Instantiation, continued

12 Size of Two-Dimensional Arrays The expression payScaleTable.length refers to the length of the payScaleTable array itself.

13 Size of Two-Dimensional Arrays The expression payScaleTable[1].length refers to the length of the array stored at row 1 of payScaleTable.

14 Subarrays An array that is part of another array is called a subarray. An array of arrays may be initialized when it is created.

15 Ragged Arrays Subarrays may be different lengths. Executing triangularArray = new double[4][ ]; for (int i = 0; i < 4; i++) triangularArray[i] = new double [i + 1]; results in an array that looks like:

16 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).

17 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.

18 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.

19 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.

20 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.

21 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.

22 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.

23 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.

24 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.

25 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.

26 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.

27 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.

28 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.

29 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.


Download ppt "Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map."

Similar presentations


Ads by Google