问题
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