Presentation is loading. Please wait.

Presentation is loading. Please wait.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.

Similar presentations


Presentation on theme: "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API."— Presentation transcript:

1 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API *ArrayList *Map

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Two-Dimensional Arrays *In Java, tabular data may be stored in a two-dimensional array. *In a two-dimensional array, two indices (one for the row and one for the column in the table) are used to refer to the array element.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Two-Dimensional Arrays *To declare our example array, we write: double[][] payScaleTable; double payScaleTable[][]; *We instantiate the array as payScaleTable = new double[4][5];

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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]

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Processing a 2D Array *Nested-for loops are useful for manipulating two-dimensional arrays. *You need one loop for each index for (int i=0; i<4; i++) { for (int j=0; j<5; j++) System.out.format( "%6.2f", payScaleTable[i][j]); System.out.println(); }

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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. *This concept can be extended to higher dimensional arrays if necessary.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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]; payScaleTable[3] = new double [5];

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Instanitating a 2D Array

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Instantiation, continued

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Size of Two-Dimensional Arrays *The expression payScaleTable.length refers to the length of the payScaleTable array itself.

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Size of Two-Dimensional Arrays *The expression payScaleTable[1].length refers to the length of the array stored at row 1 of payScaleTable.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Subarrays *An array that is part of another array is called a subarray. Also sometimes called a slice. *In Java, we get a subarray by giving just the first index of a 2D array double [] grade1 = payScaleTable[0];

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Initializing 2D arrays *An array of arrays may be initialized when it is created. *Similar to a 1D array except need to group elements into subarrays double [][] payScaleTable = {{10.50, 12.00, 14.50, 16.75, 18.00}, {20.50, 22.25, 24.00, 26.25, 28.00}, {34.00, 36.50, 38.00, 40.35, 43.00}, {50.00, 60.00, 70.00, 80.00, 99.99}};

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Changing the size of an array *The size of an array is determined when it is instantiated. *What do we do if we discover we need more elements than were allocated? 1.Use new to allocate a larger array with a temporary reference 2.Copy the elements from the original array 3.Set the original reference to the new array

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists and Maps *The java.util library contains classes for maintaining collections of objects. *These classes contain methods that make it easy to maintain collections whose size needs to change. *These classes are collectively referred to as the Java Collection Framework (JCF).

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists *The List interface is one 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. Create an instance of a class that implements List *List and the classes that implement it are examples of Generic classes - they can be implemented with elements of any object type.

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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. See Chapter 15

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Creating an ArrayList *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 ( ); You can use any type where Object appears above Using Object means the list can contain any object *Using List for the type of myList permits a quick change of the implementation class if necessary.

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists *The default constructor creates an empty list with an initial capacity of 10. *As you add elements, the capacity will grow as needed. This is useful when you don't know how many elements you will have. *Increasing the capacity of a list is not very efficient so if the necessary capacity is known, it is better to create a list with the actual capacity needed.

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Methods *The add method allows us to add objects to the list. myList.add( new Object()); *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. int count = myList.size();

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Methods *The remove method takes an element’s index as its parameter and allows us to remove an element from a list. Object o = myList.remove( 2); *The get method allows us to access objects stored in a list by giving their index position in the list. Object o = myList.get( 2);

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Methods *The iterator method returns an object that implements the Iterator interface. Iterator iter = myList.iterator(); *We can use the iterator to scan through the elements of the list without using an index. while (iter.hasNext()) System.out.println( iter.next());

25 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. List Iterators *Iterator is an interface that supports two methods. 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.

26 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Lists *The iterator method is supported by most of the collection classes. *A list cannot include primitive data values as its elements, but wrapper classes can adapt the values to acceptable list objects.

27 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Other Collection Interfaces *Maps A map consists of entries. Each entry is divided into two parts: key Value You use the key to look up a value (like looking up a word in a dictionary) *Tree A tree organizes a collection in a non-linear way Think of an organization chart


Download ppt "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API."

Similar presentations


Ads by Google