【发布时间】:2014-06-29 06:53:10
【问题描述】:
我必须编写一个名为“count”的静态方法,将 char 作为参数,该方法将返回此 char 在 Layout.ROWS(Layout 类中的字符串数组)中出现的次数。为此,我需要一个外部循环来遍历每一行,并需要一个嵌套循环来遍历每个字符。这是我尝试过的:
public static int count(char d)
{
int sum = 0;
int i = 0;
while ( i < Layout.ROWS.length && !Layout.ROWS[i].equals(d) )
{
i++;
if (Layout.ROWS[i].equals(d))
{
sum++;
}
}
return sum;
}
但它不起作用。我的输出是:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException
知道我应该怎么做吗? 这是 Layout.ROWS:
public static final String[] ROWS = {
" p ex",
" . ",
" . ",
" . ",
" . "
};
【问题讨论】:
-
“不工作”是什么意思?
-
这是我的输出:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException
标签: java arrays loops nested-loops outer-join