Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Lesson 77

Multidimensional Arrays

PRESENTED BY

Keith Vassallo icemalta.com


2D Array
- So far, we have talked about arrays as a 1-dimesion (1D) structure.
- Multi-dimensional arrays have more than one index, and represent higher
dimensions of data.
- For example, we would like to store the value of all pieces in a chess
board.
String[][] chessBoard = new String[8][8];
chessBoard[0][0] = "Black Rook";
chessBoard[0][2] = "Black Bishop";
chessBoard[0][3] = "Black King";
chessBoard[0][4] = "Black Queen";
chessBoard[0][5] = "Black Bishop";

icemalta.com
chessBoard[0][6] = "Black Knight";
chessBoard[0][7] = "Black Rook";
chessBoard[1][0] = "Black Pawn";
//....
Shortcut to Initialisation
Multidimensional arrays can also be initialised in one line.

String[][] square = {{"1","2"},{"3","4"}};

int[][][] cube = {
{{1,2}, {3,4}},
{{5,6}, {7,8}},
{{9,10}, {11,12}}
};

icemalta.com
Use
- Although multidimensional arrays can be very useful for computation, we
can also use a 1D array populated with objects instead.
- This is easier to read and understand, and will be covered soon.

icemalta.com
Great work, you’ve completed this lesson!

Next up: Using the ArrayList class.

icemalta.com
© 2011-2017 Institute of Computer Education Ltd.

The contents of this document are copyright to the Institute of Computer Education Ltd, unless otherwise stated, and must not be reproduced without permission.

Every effort has been made to trace all of the copyright holders, but if any have been inadvertently overlooked the Institute of Computer Education Ltd. will be pleased to make the necessary arrangements at the first
opportunity. Please contact us directly. While the Institute of Computer Education Ltd. has taken all reasonable care in the preparation of this work, the Institute of Computer Education Ltd. makes no representation,
express or implied, with regard to the accuracy of the information contained in this work and cannot accept any legal responsibility or liability for any errors or omissions from the work or the consequences thereof.
The reader assumes sole responsibility for the selection of these materials to achieve its intended results. Products and services that are referred to in this work may be either trademarks and/or registered
trademarks of their respective owners. The editors and author/s make no claim to these trademarks. icemalta.com

You might also like