Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a little trouble with an array for a tic tac toe board. Would I be able to use the array code that I have and alter it with what variables are needed or does a whole new array need to be made.

What I have tried:

Java
public int[][] grid(int rSize, int cSize)
    {
        
        array = new int[rSize][cSize];
        for(row = 0; row < array.length; row++)
        {
            for(col = 0; col < array[0].length; col++)
            {
                array[row][col] = 0;
            }
        }
        return array;
    }
Posted
Updated 28-Feb-18 15:40pm
v2

1 solution

Maybe you could try to use the dimensions of the array inthe loop, if that is the problem?
Java
public int[][] grid(int rSize, int cSize)
{
   array = new int[rSize][cSize];
   for(row = 0; row < rSize; row++)
   {
      for(col = 0; col < cSize; col++)
      {
         array[row][col] = 0;
      }
   }
   return array;
}

But, since an integer array will be initialized with zero values anyway, and since the method is only creating and returning the array, it would be simpler and quicker to get rid of the method.

Assuming you are using the method this way:
Java
int[][] myArray = grid(3, 3);

you could get rid of the method and simply write
Java
int[][] myArray = new int[3][3];

Technically there would only be an interest for the method if you would want it to assign specific initial values; which is not the case here.

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900