chapter 7 multidimensional arrays. defining a two dimensional array elementtype[][] arrayname; //...

19
Chapter 7 Multidimensional Arrays

Upload: angel-stanley

Post on 16-Dec-2015

220 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Chapter 7

Multidimensional Arrays

Page 2: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Defining a two dimensional array

• elementType[][] arrayName; // Java pro• elementType arrayName[][]; // C++ alternate

• For example: int[][] chessboard;• To allocate space for the array, use new:

chessboard = new int[8][8];

• Or int[][] chessboard = new int[8][8];

Page 3: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Accessing elements

• int king = 10;• chessboard[0][4] = king;• int piece = chessboard[7][3];

• Each dimension must use a pair of brackets around the index []

• Do not try chessboard[7, 3] notation

Page 4: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Initializing multiple dimensions

Page 5: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

What’s an array?

• A two-dimensional array is actually a one-dimensional array where each element is a one-dimensiona array

• An “array of arrays” is an important concept!

• Each array has its own length property• Array lengths can be different (ragged array)

Page 6: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

The length property in action

Page 7: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Rectangular versus ragged arrays

• A rectangular array has the same number of elements in each array for a given dimension

• int[][] myArray = new int[5][7];• myArray has 5 x 7 = 35 elements, with 7

elements in each of the 5 rows• A ragged array has varying numbers of

elements in a given dimension, as illustrated on the next slide:

Page 8: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Ragged array definition

Page 9: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

The contents of a ragged array

Page 10: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Another array initialization example

Page 11: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Passing a two-dimensional array as an argument to a method

Page 12: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Multiple Dimensional Arrays

• An array of arrays of arrays of….• Each dimension can consist of rectangular or

ragged arrays• Each dimension is enclosed with a pair of

brackets• Maximum number of dimensions is nominally

255 for the VM*

Page 13: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate
Page 14: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Three-dimensional ragged array

Page 15: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Lab 2

• Write a program to create a chessboard stored as a two-dimensional character array

• Chess is played on an 8 x 8 square board with a total of 64 squares

• Empty squares can be represented by a space• Abbreviate pieces as ‘R’ = rook, ‘N’ = knight,

‘B’ = Bishop, ‘Q’ = queen, ‘K’ = king, ‘P’ = pawn• Initialize the array according to the diagram

Page 16: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

What the array dimensions mean

• The columns will be the first array dimension• Label columns ‘a’ – ‘h’• Column a is the 0 index, h is the 7 index

• The rows will be the second array dimension• Number rows 1-8• Row 1 is the 0 index, 8 is the 7 index

Page 17: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

What to do and turn in

• Write a method that creates a two-dimensional character array, initializes it with the starting chessboard as shown in the diagram, and returns the array

• Write a method that prints the chess board contents and the row/column labels

• Turn in a screen shot of the chess board• Turn in a print-out of your program

Page 18: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

What the chessboard looks like:

Page 19: Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate

Extra Credit (10%)

• Write a user interface that makes a move• The notation is the letter/number of the start

location and the letter/number of the end location (e.g. “d1 g4” would move the white queen diagonally 3 squares)

• Allow any move (don’t check legality)• Print an updated view of the board showing

the pieces after the move