Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: Part 3 Collections and Two-dimensional arrays.

Similar presentations


Presentation on theme: "Chapter 8: Part 3 Collections and Two-dimensional arrays."— Presentation transcript:

1 Chapter 8: Part 3 Collections and Two-dimensional arrays

2 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The Collections Framework: ArrayLists Two-Dimensional Arrays Common Programming Errors

3 3

4 4 The Collections Framework: ArrayLists Array –Data structure of choice for fixed-length collections of data that are related Many programming applications require variable- length lists –Java provides a set of classes referred to as the collections framework –Provides seven different types of generic data structures

5 5

6 6 The Collections Framework The Collections class: –Supports container classes –Provides functions for: Searching Sorting Random shuffling Reverse-ordering

7 7

8 8

9 9

10 10 The Iterator Class Similar to an array’s index Generalized index that keeps track of object’s position within a container For some classes it provides the primary means of accessing individual elements Obtaining an iterator: –Iterator iter = x.iterator();

11 11 Parallel Arrays as Records Parallel arrays: –Corresponding data in a record resides in the same position in more than one array –Required in earlier programming languages that only supported array data structures –Can combine parallel elements in an object Store objects in a one-dimensional array

12 12 Two-Dimensional Arrays Consist of both rows and columns of elements Sometimes called tables Example declaration: –int val[][]; Example allocation: –val = new int[3][4]; Elements are identified by position in an array

13 13 Two-Dimensional Arrays (continued) Can be initialized from within declaration statements: –int val[][] = {{8,16,9,52}, {3,15,27,6}, {7,25,2,10}}; Number of columns need not be the same for each row May be displayed by: –Individual element notation –Using loops Usually nested loops

14 14

15 15 Two-Dimensional Array Length val.length –Provides the number of rows in the array referenced by val val[i].length –Provides the number of columns in the ith row of val array

16 16 Passing Two-Dimensional Arrays Identical to passing a one-dimensional array –The called method receives access to the entire array

17 17 Advanced Dimensioning Capabilities Can create two-dimensional arrays where each row has a different number of columns To create: –Initialize a list that explicitly lists values for each row –Or use the new operator and place values into the newly created array

18 18 Larger Dimensional Arrays Can have any number of array dimensions Similar to creating and allocating two-dimensional arrays Declaration: int val[][][]; val = new int[3][2][2];

19 19

20 20 Common Programming Errors Forgetting the empty bracket pairs when declaring an array’s name Declaring an array reference variable using explicit dimension sizes Using a subscript that references a nonexistent array element Not using a large enough counter value in a for loop counter to cycle through all array elements

21 21 Summary One-dimensional array: –Data structure –Stores list of values of same data type Array elements: –Stored in contiguous locations in memory –Referenced using the array name and a subscript Such as num[22]

22 22 Summary (continued) Two-dimensional array is declared by providing: –Data type –Reference variable name –Two sets of empty bracket pairs after the array’s name Arrays may be initialized when they are declared Collections framework –Set of classes providing generic data structures


Download ppt "Chapter 8: Part 3 Collections and Two-dimensional arrays."

Similar presentations


Ads by Google