【发布时间】:2014-04-15 11:28:51
【问题描述】:
我正在尝试编写一个接收字符串和数组并打印出以下内容的方法...
--- Name is "Abe"---
Adorable
Beautiful
Elegant
--- Name is "bob"---
Beautiful
Outstanding
Beautiful
我的代码...
public static void printReport(String name, String[] sortedArray)
{
System.out.println(name);
char c = '\0';
for (int i = 0; i <name.length()-1;i++)
{
c = name.charAt(i);
int index = c-'A';
System.out.println(sortedArray[index]);
}
}
}
其中 name 是...井名,而 sortedArray 是按字母顺序排列的 26 个形容词的数组。我正在尝试使用 chars 的数值来打印出正确的形容词。我在
上遇到错误System.out.println(sortedArray[index]);
而且似乎无法弄清楚原因。非常感谢任何和所有帮助。
【问题讨论】:
-
您可能想尝试的是使用映射对象,其中字符映射到字符串,这样您就不必担心基于字符的排序和创建索引。另外,您看到了什么错误?
-
错误是什么? ArrayIndexOutOfBoundException??
标签: java for-loop methods indexing char