我正在阅读java中的matrx,我在我的课程页面中找到了这个方法:
public int getSeatStatus(int row, char ch) { // Returns the status of the seat (row, ch)
return seats[row][(ch - ’A’)].getStatus();
}我不明白这个方法是怎么工作的。正如您所看到的,ch不是一个索引,那么为什么他们要将它用作索引呢?
我们可以用另一种简单的方式重写这段代码吗?例如,我尝试这样重写它:
public int getSeatStatus(int row, char ch){
int col = 0;
if (ch == 'A') {
col=0;
} else if (ch == 'B') {
col=1;
}
return s[row][col].getStatus();
}我做得对吗?谢谢
发布于 2016-05-25 19:59:48
在Java语言中,char是一种数值类型。当你使用char - ’A’时,你使用的是unicode code point。在'A' - 'A'的情况下,结果为0,当'B' - 'A'结果为1时,依此类推。它给出数组索引。
https://stackoverflow.com/questions/37436110
复制相似问题