arrays1 © 2014 goodrich, tamassia, goldwasser presentation for use with the textbook data...

12
Arrays 1 Arrays © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014

Upload: kerry-doyle

Post on 17-Jan-2016

244 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Arrays 1

Arrays

© 2014 Goodrich, Tamassia, Goldwasser

Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014

Page 2: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Arrays 2

Array Definition An array is a sequenced

collection of variables all of the same type. Each variable, or cell, in an array has an index, which uniquely refers to the value stored in that cell. The cells of an array, A, are numbered 0, 1, 2, and so on.

Each value stored in an array is often called an element of that array.

A0 1 2 ni

© 2014 Goodrich, Tamassia, Goldwasser

Page 3: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Arrays 3

Array Length and Capacity Since the length of an array determines the

maximum number of things that can be stored in the array, we will sometimes refer to the length of an array as its capacity.

In Java, the length of an array named a can be accessed using the syntax a.length. Thus, the cells of an array, a, are numbered 0, 1, 2, and so on, up through a.length−1, and the cell with index k can be accessed with syntax a[k].

a0 1 2 nk

© 2014 Goodrich, Tamassia, Goldwasser

Page 4: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Declaring Arrays (first way) The first way to create an array is to use an

assignment to a literal form when initially declaring the array, using a syntax as:

The elementType can be any Java base type or class name, and arrayName can be any valid Java identifier. The initial values must be of the same type as the array.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 4

Page 5: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Declaring Arrays (second way) The second way to create an array is to

use the new operator. However, because an array is not an instance

of a class, we do not use a typical constructor. Instead we use the syntax:

new elementType[length] length is a positive integer denoting the

length of the new array. The new operator returns a reference to

the new array, and typically this would be assigned to an array variable.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 5

Page 6: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Arrays of Characters or Object References An array can store primitive elements,

such as characters.

An array can also store references to objects.

© 2014 Goodrich, Tamassia, Goldwasser Arrays 6

Page 7: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Java Example: Game Entries A game entry stores the name of a player and her best score so far in a

game

© 2014 Goodrich, Tamassia, Goldwasser Arrays 7

Page 8: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Java Example: Scoreboard

© 2014 Goodrich, Tamassia, Goldwasser Arrays 8

Keep track of players and their best scores in an array, board

The elements of board are objects of class GameEntry Array board is sorted by score

Page 9: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Arrays 9

Adding an Entry To add an entry e into array board at index

i, we need to make room for it by shifting forward the n - i entries board[i], …, board[n – 1]board

0 1 2 ni

board0 1 2 ni

0 1 2 nei

© 2014 Goodrich, Tamassia, Goldwasser

board

Page 10: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Java Example

© 2014 Goodrich, Tamassia, Goldwasser Arrays 10

Page 11: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Arrays 11

Removing an Entry To remove the entry e at index i, we need to fill

the hole left by e by shifting backward the n - i - 1 elements board[i + 1], …, board[n – 1]

0 1 2 ni

0 1 2 nei

0 1 2 ni

© 2014 Goodrich, Tamassia, Goldwasser

board

board

board

Page 12: Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,

Java Example

© 2014 Goodrich, Tamassia, Goldwasser Arrays 12