如何将对象2d数组(x,y)位置传递给int

时间:2014-05-13 22:37:18

标签: arrays string object methods int

我需要将某个位置的对象数组传递给int。例如:

 private Object[][] singleplay = {{"#","Name","Score"},{"1","----------","10"},{"2","----------","20"}};

private int getnum1;
private int getnum2;

这里,我需要取每行对象数组的第三个位置。例如:

getnum1 = singleplay[1][2] // So getnum1 will be 10
getnum2 = singleplay[2][2] // getnum2 will be 20.

所以我需要从每个2d“y”位置的第3个“x”位置获取Int

1 个答案:

答案 0 :(得分:0)

我认为这样做会。另外,Object[][]不对。

private String[][] singleplay = {{"#","Name","Score"},{"1","----------","10"},{"2","----------","20"}};

/**
 * Returns the score.
 *
 * @param id
 *        The index of the entry in the array table.
 *
 * @throws ArrayIndexOutOfBoundsException
 *         When the destination relative position is out of bounds.
 *
 * @return The score, 0 if it's not an integer.
 */
int getScore(int id) throws ArrayIndexOutOfBoundsException{
    // Our destination is the third element of the id th
    // first-dimensional array element:
    final String scoreRaw=singleplay[id][2];
    // Try to convert it to an actual score.
    return scoreRaw.matches("^-?\\d+") ? Integer.parseInt(scoreRaw) : 0;
}