Two-Dimensional Arrays

In Java


Linear & 2-D arrays

1.   The arrays we have looked at so far have stored lists of data

2.   Sometimes we would like to store a table of data

3.   When we do this we use two subscripts: one for rows, and one for columns.

4.   The row subscript comes first, then the column


2-D arrays

Suppose we want an three-by-four table to hold integers

We declare it this way

     int [ ] [ ] table = new int [3][4];

Actually, a better way is to declare constants, then use them to determine the size of the 2-D array

final int ROW = 3; // constants

final int COL = 4;

int [ ] [ ] table = new int [ROW][COL];


TicTacToe class

    Suppose we want to write a TicTacToe game

    Run the game first, then figure out how to write it

    We need

          a 3 X 3 board

          A constructor to initialize the 2-D array to spaces

          A toString method to output the board

          A method to set the moves


TicTacToeTester

    Our players will be “x” or “o”

    We prompt the user for the row and column where they want to put their mark.

    After we get the row and column where a player wants their mark, we call the method to set the mark.

    We output after each move.

    The players take turns, first “x”, then “o”.


Iterating through arrays

The classic way to iterate through an array is to use a for loop

String[ ] aray = new String[10];

for( I = 0; I < aray.length; i+++)

{ do something}

With the 5.0 version a Java, an enhanced for loop has been introduced

for(String num : aray)

{ num will be each element of the array from 0 to aray.length)


Using the enhanced for loop

sum = 0;

int[ ] myInt = {45,34,67,12,98,32,78,44,77,11}

for(int num : myInt)

sum = sum + num;

String[ ] myString = {"bat", "cat", "rat", "mat",

"sat", "at", "dot", "zat", "nat", "pat"};

for(String num : myString)

System.out.println(num + “ “)


A Little help with the lab

    We have classes Card and DeckOfCards, and DeckOfCardsTest

    We want to

          Deal a hand of five cards

                This is done in the tester class by creating an array of five cards, and dealing five cards to it

          Test to see if we have a pair

                This is done by calling a method in main to do the test

                The method to test for pairs will be in the DeckOfCards class


Dealing the hand in main

In main, addition to the DeckOfCards array, you need an array of five cards to hold the hand

public final int NUM = 5;

Card[ ] myHand = new Card[NUM];

Now, dealt the cards into the array

for(i = 0; i < myHand.length; i++)

myHand[i] = myDeckOfCards.dealCard( );

Now you make a method call to find out if there is a pair


Testing for a pair in DeckOfCards

    The hand that was dealt in main either has a pair or it doesn’t

    So our method will return a boolean

    We must know what is in the hand, so our method will take as an argument the hand dealt in main

    The method header looks like this:

public boolean havePair(Card[ ] hand)


The method havePair

public boolean havePair(Card[ ] hand)

    We must be able to see the faces of the cards to see if we have a pair

          Remember, each element of the hand array is of type Card, and can call Card methods

    To see the first card in the hand we say

     hand[0].getFace( )

    This will return a String, which we can compare with the second card in the hand this way

     hand[0].getFace( ).equals(hand[1].getFace( ));

    Of course, you use a loop so you don’t actually hard code the indices in




Calling the method havePair from main

So far, we had in main

Card[ ] myHand = new Card[NUM];

for(I = 0;I < myHand.length; i++)

myHand[i] = myDeckOfCards.dealCard( );

    havePair is going to return a boolean

    So the call would look like this

           boolean bool = havePair(myHand);

    This will return true or false, which you can use to output the correct response


Passing Arrays to Methods

    Suppose we have two arrays in main, and we want to call some methods to manipulate them

    We must tell the methods which array to work on

    The situation is this

           int [ ] aray1= { 1,3,5,7,9}

           int[ ] aray2 = {2,4,6,8,10}




The PassArray class

    This class illustrates three things

    How to pass an entire array

    How to pass a single element of an array

    And what happens if the element is a primitive

    Use of the enhanced for loop


Pass by value & pass by reference

    When an address is passes as an argument, the method can actually access the element(s) at the address and change them

    Any object name is an address, not a value

    When a primative is passes as an argument, the called method gets a copy of it, so cannot change it in the calling program.