-5

enter image description hereIm trying to create an 8x8 array and i know that according to java that array[0][0] is the index of the first row, first column. I want to rename this to some name 'a1' similary array1[0] as 'a2' array[2][0] as 'a3' The code below is what I want to do but I am not sure if there exists a syntax for that. Correct me if I'm wrong but if there is a chance to rename my array, that will make it so much easier for me. Thanks in advance.

char[][] cb;
char a=8;
char b=8;
cb= new char[a][b];
cb[0][0]=name('a1');
cb[1][0]=name('a2');
cb[2][0]=name('a3');
...
  ...
cb[5][7]=name('h6');
cb[6][7]=name('h7');
cb[7][7]=name('h8');


//Then I save some elements in each index with the new name

I've attached the chessboard image, I get an input as Rf1 which means my rook is at cell f1 but in array language its [7][5] right? So i want to know if I can do that change somehow.

  • 2
    Possible duplicate of [Assigning variables with dynamic names in Java](https://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – Hovercraft Full Of Eels Aug 19 '18 at 02:54
  • 2
    Sounds like a **XY-problem**. Please clarify what you want to do with this, there's likely a way better approach. – Zabuzard Aug 19 '18 at 02:54
  • 2
    If you're trying to name the variable itself, then no, this cannot be done. If you're trying to assign a char to the array that is not a char, then I have no idea what you're really trying to do. – Hovercraft Full Of Eels Aug 19 '18 at 02:54
  • You can't directly "rename" the cells of your array, but you _can_ turn your spreadsheet-like names into the corresponding pairs of integer subscripts. – Kevin Anderson Aug 19 '18 at 03:06
  • 1
    Possible duplicate of [Assigning variables with dynamic names in Java](https://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) –  Aug 19 '18 at 03:13
  • edited the post now, please check image to understand my question better – NewLearner Aug 19 '18 at 03:34

1 Answers1

1

You can't make it so the variables in your code have those names, but what you can do is write a method that converts the string input, such as f1 into the indexes required. The basic approach is to separate the column (a through g) and rank (1 through 8) and then map those to row and column. In the (pseudo-)code below I'll assume index [0,0] is at the bottom left, corresponding to the a1 square. The code assumes the input is a valid position and does not do any range checking, that is left to you.

public static class CellIndex {
    int row;  
    int col;
    public CellIndex(int row, int col) {
        this.row = row;
        this.col = col;
    }
    // getters, etc.
}
...
public CellIndex mapPositionToIndex(String position) {
    int col = (int)(position.charAt(0) - 'a');
    int row = (int)(position.charAt(1) - '1');
    return new CellIndex(row,col);
}

The CellIndex class encapsulates a row/column cell position in your array.

The method mapPositionToIndex takes in a string with a position designator and converts it to an array index. Input a1 converts to [0,0] and h8 converts to [7,7]. If you want to start with the top left corner you can adjust the calculation, but the principle is the same.

There are many other ways to approach this problem. If you have classesBoard and Cell, which model the board, you could have a Board method that returns the appropriate Cell object when given a position, hiding all the internal details from the users of the class, i.e.

Class Cell {
    // The model of a single cell on a chessboard
}
Class Board {
    ...
    public Cell getCell(String position) {
        // Interpret the position and return the corresponding Cell object
    }
    ...
}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190