简体   繁体   中英

Passing a 2d array from one method to another

Programming language: Java

editor: Vim

I been trying to do an assignment that includs a 2d array. I've been struggling on it pretty bad since this is the second time I've used any kind of arrays. Anyway, i just got some questions that I hope someone here can clear up for me.

1) is this is how you would pass a double array to another method?

this is my boolean array:

boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];

this is how I have been trying to pass it.

countNeighbors(TempGrid[][]);

this is method accepting the array:

 public static int countNeighbors ( final boolean[][] grid, final int row, final int col )

however, when I compile i get an error saying:

error: '.class' expected
     countNeighbors(TempGrid[][]);
                                ^
1 error

I did some research on the .class error like:

- http://www.dreamincode.net/forums/topic/70299-class-expected-error/

- http://stackoverflow.com/questions/12309220/class-error-in-java-applet

- http://www.daniweb.com/software-development/java/threads/213357/pass-2-dimensional-array-into-method

and various other sites/forums. The solution they presented either didn't work or cause more issues with my program.

btw this is the entire method:

 public static void genNextGrid ( boolean[][] grid )
{

     boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];

     TempGrid[GRIDSIZE][GRIDSIZE] = grid[GRIDSIZE][GRIDSIZE];

     countNeighbors(TempGrid);

    for(int row = 1; row < 18; row++)
{

        countNeighbors(row);

        for(int col = 1; col < 18; col++)
{

            countNeighbors(col);

            if(n == 3)
            {
                TempGrid[row][col] = true;
            }
            else if(n == 2 || n == 3)
            TempGrid[row][col] = true;
            }
            else
            {
                TempGrid[row][col] = false;
            }

        }
    }


}

I've attempted in removing [][] from countNeighbors(TempGrid[][]); so it would look like:

countNeighbors(TempGrid);

but gave me 3 errors

 error: method countNeighbors in class Life cannot be applied to given types;
     countNeighbors(TempGrid);
     ^
 required: boolean[][],int,int
found: boolean[][]
reason: actual and formal argument lists differ in length


error: method countNeighbors in class Life cannot be applied to given types;
        countNeighbors(row);
        ^
required: boolean[][],int,int
found: int
reason: actual and formal argument lists differ in length


 error: method countNeighbors in class Life cannot be applied to given types;
            countNeighbors(col);
            ^
  required: boolean[][],int,int
 found: int
 reason: actual and formal argument lists differ in length

-Thanks for your help in advance

Yes the 2nd way is the correct way ie

 countNeighbors(TempGrid);

And thee later error is saying you the number of arguments required doesn't match, as

public static int countNeighbors ( final boolean[][] grid, final int row, final int col )

The countNeighbors method expects 3 parameters, and you are just passing 1. Verify what you need to pass to this method, or remove them if not required. Also it returns a int , you need to assign it back to something for further use. I envisage it's the variable n , you intend to use it for.

You need to call the method in the innermost loop as

for(int row = 1; row < 18; row++) {
    for(int col = 1; col < 18; col++) {
        n = countNeighbors(TempGrid,row,col);
        ...

    }
}

You only need the [][] when you declare the variable. After that only use the name (ie TempGrid)

So if your method takes a 2D array and is named foo, then: foo(TempGrid) will pass a reference to TempGrid to foo.

In Java all is object, thus your method must be declared inside a class.

IMHO your method should look something like this:

public static void genNextGrid ( boolean[][] grid )
    {

        boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];

        TempGrid = grid;


        for(int row = 1; row < 18; row++)
        {


            for(int col = 1; col < 18; col++)
            {

                int n = countNeighbors(TempGrid, row, col);

                if(n == 3)
                {
                    TempGrid[row][col] = true;
                }
                else if(n == 2 || n == 3)
                    TempGrid[row][col] = true;
                else
                {
                    TempGrid[row][col] = false;
                }
            }

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM