ArrayIndexOutofBoundsException 错误但不知道为什么

发布于 2024-10-04 01:40:06 字数 1593 浏览 3 评论 0原文

所以我正在为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榆西 2024-10-11 01:40:06

在您的第一个 substring 方法中,该行

range.miniscule[i] = this.miniscule[i];

最有可能是可疑的。我希望您确实希望

range.miniscule[i - start] = this.miniscule[i];

String 有子字符串方法来完成您在这里所做的事情,但我认为您这样做是为了学习,而不是为了生产用途而重新发明字符串处理。

如果您使用的是 Java 6,您可能还需要查看 Arrays.copyOfRange(T[] ts, int i, int i1) 方法,该方法可以完成您正在执行的大部分工作在您的 substring 方法中。

In your first substring method, the line

range.miniscule[i] = this.miniscule[i];

is the most likely suspect. I expect you really want

range.miniscule[i - start] = this.miniscule[i];

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 your substring methods.

回心转意 2024-10-11 01:40:06

Don 已经指出了您的问题所在,这里有更多想法供您

参考字符串“hello”的示例

  • Ok:start = 0,end = 5 => i = 0,range.length = 5 且 range.miniscule[i] 在范围内
  • Err: start = -1, end = 0 => i = -1,range.length = 1 但 range.miniscule[i] 超出 -1 的范围
  • Err: start = 4, end = 5 =>我= 4,
    range.length = 1 但 range.miniscule[i] 超出范围
  • Err: start = 0, end = 7 => i = 0,this.miniscule[i] 对于 5 和 6 会失败

Don already pointed out where your problem lies, here are couple more thoughts for you

Example for string "hello"

  • Ok: start = 0, end = 5 => i = 0, range.length = 5 and range.miniscule[i] is inside range
  • Err: start = -1, end = 0 => i = -1, range.length = 1 but range.miniscule[i] is out of range for -1
  • Err: start = 4, end = 5 => i = 4,
    range.length = 1 but range.miniscule[i] is out of range
  • Err: start = 0, end = 7 => i = 0, this.miniscule[i] would fail for 5 and 6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文