我如何传递一个对象的二维数组(X,Y)的位置为一个int [英] How do I pass an object 2d array (x,y) position into an int

查看:109
本文介绍了我如何传递一个对象的二维数组(X,Y)的位置为一个int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在某个位置为int传递一个对象数组。例如:

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.

所以,我需要的诠释从第3每个二维Y位置的X的位置

推荐答案

我想这会做到这一点。此外,对象[] [] 是不正确的。

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

这篇关于我如何传递一个对象的二维数组(X,Y)的位置为一个int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆