Hi,

I am new to Java, but have been programming in C++ for a few years now. I am working on a project where I have to write a class to implement a gameboard. To do so, I'm using a 2D array of objects. I have a method that swaps two lines in the 12 X 12 matrix. What I have done is set up a new 1D array of the object type and temporarily stored one of the rows/columns to be swapped into this array, I then copy it's contents into the other row/column. My question is, when I am done with this tempArray of objects, must I deallocate it somehow, or does garbage collector take care of this??

Here is my code for this method:

public void swapLines(char xORy, int line1, int line2){

        Tile [] tempLine = new Tile [12];

        if(xORy == 'X'){

            for(int i = 0; i <= 11; i++){

                tempLine[i] = new Tile(board[i][line1].getLetter(), board[i][line1].getValue());

            }

           for(int i = 0; i <= 11; i++){

               board[i][line1] = board[i][line2];

           }

           for(int i = 0; i <= 11; i++){

               board[i][line2] = tempLine[i];

           }

        }

        if(xORy == 'Y'){

            for(int i = 0; i <= 11; i++){

                tempLine[i] = new Tile(board[line1][i].getLetter(), board[line1][i].getValue());

            }

            for(int i = 0; i <= 11; i++){

               board[line1][i] = board[line2][i];

           }

           for(int i = 0; i <= 11; i++){

               board[line2][i] = tempLine[i];

           }
        }
    }

Any pointers would be greatly appreciated (no pun intended).

Thanks,

Gadgetman_53

Garbage collector does this for you periodically or when it detects that they are no longer being used. Usually the reference to a variable is lost when the variable goes out of scope.

If you want to do it explicitly set the value of the variable to null(I guess).

Thanks for the answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.