ArrayIndexOutofBoundsException 错误但不知道为什么
所以我正在为“MiniString”创建一个类,它充满了对象MiniString的实例方法,每个MiniString都有一个实例变量,它是一个char[]。在测试我的方法时,我找不到 substring() 方法出错的地方。有两种子字符串方法,一种采用 int 参数,另一种采用两个 int 参数。我不断收到有关 one int 参数方法的错误。 substring 方法应该返回一个新的 MiniString,该新的 MiniString 由 int 参数指定的目标 Ministring 中的位置和目标 MiniString 的末尾之间的字符组成。我在 JUnit 测试器中不断遇到的错误如下:
java.lang.ArrayOutofBoundsException:22
at MiniString.substring(MiniString.java:141)
at MiniString.substring(MiniString.java:159)
这是对象 MiniString 的构造函数:
private char[] miniscule;
MiniString(char[] array){
int i = 0;
miniscule = new char[array.length];
while (i < array.length){
miniscule[i] = array[i];
i++;
}
}
MiniString(String string){
int i = 0;
miniscule = new char[string.length()];
while (i < string.length()){
this.miniscule[i] = string.charAt(i);
i++;
}
}
这是两个 substring() 方法的代码:
public MiniString substring(int start, int end){
int i = start;
if (end > start){
char[] temp = new char[end - start];
MiniString range = new MiniString(temp);
while (i < end){
range.miniscule[i] = this.miniscule[i];
i++;
}
return range;
}
else{
char[] temp = new char[1];
MiniString range = new MiniString(temp);
range.miniscule[0] = 0;
return range;
}
}
public MiniString substring(int position){
int start = position;
int end = this.miniscule.length;
char[] temp = new char[end - start];
MiniString output = new MiniString(temp);
output = substring(start, end);
return output;
}
感谢您的帮助!
So i am creating a class for "MiniString" which is just full of instance methods for the object MiniString, every MiniString has an instance variable that is a char[]. When testing my methods, I can't find where I am going wrong with my substring() method. There are two substring methods where one takes a parameter of int and the other takes two int parameters. I keep getting the error on the one int parameter method. The substring method is supposed to return a new MiniString formed of the characters between the position in the target Ministring specified by the int parameter, and the end of the target MiniString. The error I keep getting in my JUnit Tester is the following:
java.lang.ArrayOutofBoundsException:22
at MiniString.substring(MiniString.java:141)
at MiniString.substring(MiniString.java:159)
Here are my constructors for the object MiniString:
private char[] miniscule;
MiniString(char[] array){
int i = 0;
miniscule = new char[array.length];
while (i < array.length){
miniscule[i] = array[i];
i++;
}
}
MiniString(String string){
int i = 0;
miniscule = new char[string.length()];
while (i < string.length()){
this.miniscule[i] = string.charAt(i);
i++;
}
}
and here is the code for the two substring() methods:
public MiniString substring(int start, int end){
int i = start;
if (end > start){
char[] temp = new char[end - start];
MiniString range = new MiniString(temp);
while (i < end){
range.miniscule[i] = this.miniscule[i];
i++;
}
return range;
}
else{
char[] temp = new char[1];
MiniString range = new MiniString(temp);
range.miniscule[0] = 0;
return range;
}
}
public MiniString substring(int position){
int start = position;
int end = this.miniscule.length;
char[] temp = new char[end - start];
MiniString output = new MiniString(temp);
output = substring(start, end);
return output;
}
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的第一个
substring
方法中,该行最有可能是可疑的。我希望您确实希望
String
有子字符串方法来完成您在这里所做的事情,但我认为您这样做是为了学习,而不是为了生产用途而重新发明字符串处理。如果您使用的是 Java 6,您可能还需要查看 Arrays.copyOfRange(T[] ts, int i, int i1) 方法,该方法可以完成您正在执行的大部分工作在您的
substring
方法中。In your first
substring
method, the lineis the most likely suspect. I expect you really want
String
has substring methods that do approximately what you're doing here, but I presume you're doing this to learn, rather than reinventing string handling for a production use.If you're using Java 6, you might also want to look at the
Arrays.copyOfRange(T[] ts, int i, int i1)
method, which does most of the work you're doing in yoursubstring
methods.Don 已经指出了您的问题所在,这里有更多想法供您
参考字符串“hello”的示例
range.length = 1 但 range.miniscule[i] 超出范围
Don already pointed out where your problem lies, here are couple more thoughts for you
Example for string "hello"
range.length = 1 but range.miniscule[i] is out of range