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

Chapter 8: Part 3 Collections and Two-dimensional arrays

Embed Size (px)

DESCRIPTION

3

Citation preview

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

Chapter 8: Part 3

Collections and Two-dimensional arrays

Page 2: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

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

3

Page 4: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

Page 5: Chapter 8: Part 3 Collections and Two-dimensional arrays

5

Page 6: Chapter 8: Part 3 Collections and Two-dimensional arrays

6

The Collections Framework

• The Collections class:

– Supports container classes

– Provides functions for:

• Searching

• Sorting

• Random shuffling

• Reverse-ordering

Page 7: Chapter 8: Part 3 Collections and Two-dimensional arrays

7

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

8

Page 9: Chapter 8: Part 3 Collections and Two-dimensional arrays

9

Page 10: Chapter 8: Part 3 Collections and Two-dimensional arrays

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();

Page 11: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

Page 12: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

Page 13: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

Page 14: Chapter 8: Part 3 Collections and Two-dimensional arrays

14

Page 15: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

Page 16: Chapter 8: Part 3 Collections and Two-dimensional arrays

16

Passing Two-Dimensional Arrays

• Identical to passing a one-dimensional array

– The called method receives access to the entire array

Page 17: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

Page 18: Chapter 8: Part 3 Collections and Two-dimensional arrays

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];

Page 19: Chapter 8: Part 3 Collections and Two-dimensional arrays

19

Page 20: Chapter 8: Part 3 Collections and Two-dimensional arrays

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

Page 21: Chapter 8: Part 3 Collections and Two-dimensional arrays

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]

Page 22: Chapter 8: Part 3 Collections and Two-dimensional arrays

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