How do I pass an object 2d array (x,y) position into an int

試著忘記壹切 提交于 2019-12-08 14:26:01

问题


I need to pass an object array at a certain position to an int. For example:

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

private int getnum1;
private int getnum2;

Here, I need to take the third position of the object array of each row. Example:

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

So I need the Int from the 3rd "x" position of each 2d "y" position.


回答1:


I think this will do it. Also, Object[][] isn't right.

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;
}


来源:https://stackoverflow.com/questions/23642762/how-do-i-pass-an-object-2d-array-x-y-position-into-an-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!