Jannes
Jannes

Reputation: 15

how to create method with 2d array

I am having difficulties with creating a method containing a 2 array.I understand the concept of how to create a method and creating a 2d array, but I am struggling with combining the two. I have to create a grid from 32 to -31 (see code), but have to display using a method using JOptionPane. Mainly I get lost with all the [], {} and () in creating and calling the method. Who can help me by telling ( or explaining ) how to create and call a 2d-arry method?

Thank you very much.

   import javax.swing.JOptionPane;//not beeing used yet, have to create JOptionPane

    public class Inzend2 {
      public static void main(String[] args) {
    //over here I want to call a method for printing the blastTable in 
    //JOptionPane( for example printArray).
    //Having difficulties on creating a method containing a 
    //two-dimensional array and how to call this method

    //methode for creating blastTable, a grid of 8x8, 
    //start at 32, ends -31. How to make a method with a 2d-array?
    // Am getting lost in all the [],{} and ()
    int [][] blastTable = new int [8][8];

    int lengteArray1 = blastTable.length;//is not beeing used,
    // but created for my understanding on how to get the lenght of 
    //more dimensional array
    int lengteArray2 = blastTable [0].length;
    int beginpunt = 32;

    for ( int x = 0; x < blastTable.length; x++) {
        for ( int y = 0; y < lengteArray2; y++){
                    blastTable [x][y] = beginpunt;
                    beginpunt--;
                    System.out.print(blastTable[x][y]+ " ");
        }
        System.out.print("\n");
    }
}

}

Upvotes: 0

Views: 2074

Answers (3)

CoderBrain
CoderBrain

Reputation: 103

More or less whole of your code is correct.

Instead of printing the 2D array in console, store it in a String variable. You can then return this variable and use it to display your text in JOptionPane.

public String something() {
//over here I want to call a method for printing the blastTable in 
//JOptionPane( for example printArray).
//Having difficulties on creating a method containing a 
//two-dimensional array and how to call this method

//methode for creating blastTable, a grid of 8x8, 

int [][] blastTable = new int [8][8];

int lengteArray2 = blastTable [0].length;
int beginpunt = 32;
String a="";
for ( int x = 0; x < blastTable.length; x++) {
    for ( int y = 0; y < lengteArray2; y++){
                blastTable [x][y] = beginpunt;
                beginpunt--;
                a+=beginpunt+"  ";
    }
    a=a+"\n";
}
System.out.println(a);
return a;

}

The string a can be returned

Now call this method from required place and display the String in the JOptionPane.

String a = something();
JOptionPane.showMessageDialog(null,a);

Hope this solves your problem

Upvotes: 1

jacks
jacks

Reputation: 4783

In the following, createArray() could return an int[][] but here it is just set it as a class variable:

public class TwoDArray {

    private static int[][] board;

    public static void main(String[] args) {

        createBoard();

        // add some pieces to some arbitrary locations
        // REMEMBER, array indexes are zero based
        addToBoard(0,0); // row 1, col 1
        addToBoard(2,5); // row 3, col 6
        addToBoard(3,7); // row 4, col 8
        addToBoard(7,3); // row 8, col 4

        // remove 1 piece
        removeFromBoard(0,0);

        for(int x = 0; x < board.length; x++) {
            for(int y = 0; y < board.length; y++) {
                System.out.println("position (row/col) :: " + (x + 1) + "/" + (y + 1) + " = " + board[x][y]);
            }
        }
    }

    private static void createBoard() {
        if(board == null) {
            board = new int[8][8];
        }
    }

    /*
     * value of 0 = empty square
     * value of 1 = play piece in square
     */
    private static void addToBoard(int rowPos, int colPos) {
        board[rowPos][colPos] = 1;
    }

    private static void removeFromBoard(int rowPos, int colPos) {
        board[rowPos][colPos] = 0;
    }

}

Upvotes: 0

user2891133
user2891133

Reputation: 329

private int[][] myMethodName() {

    return myDoubleIntArray;

}

Upvotes: 0

Related Questions