繁体   English   中英

数组索引越界Java程序

[英]Array Index out of bounds Java program

有人可以向我解释为什么我得到一个数组越界错误吗?

longestStreak : 布尔数组 -> 整数
目的:计算输入参数值中连续出现的最长连续真值的长度
输入:values 是长度至少为 1 的布尔值的非空数组

output : 输出在输入数组中找到的最大连续真数

import java.util.Arrays;

public class Problem3 {

  public static void main (String[] args) { 
    boolean[] test = new boolean[] {true, false, true, true, false};
    int[] o = longestStreak(test);
    System.out.println(o);
  }

  public static int[] longestStreak(boolean[] values) {
    int streak = 0; 
    int max = 0;
    int arrsize = 0; 
    for (int i = 0; i < values.length; i++) {
      if (values[i]) {   
        streak++;
        max = streak;
        arrsize++;
      }
      else {           
        streak = 0;
      }
    }

    int[] output = new int[arrsize];

    for (int i = 0; i < values.length; i++) { 
      for (int z = 1; z < values.length; z++) {
        if (values[i]) {
          i = output[z];
        }
      }
    }
    return output;
  }

}

我猜是以下内容:

values[i]true ,行: i = output [z]; 将尝试达到可能大于2的索引。

我认为有问题的代码行是

i = output[z];

原因是因为我认为您可以将 i 设置为大于 values.length 的值,所以我重新阅读了您的问题陈述。 下面的代码将为您修复它并且它有效。 只需将其插入:导入 java.util.Arrays;

  public class Test {


  public static void main (String []args) { 


 boolean[] test = new boolean[] {true, false, true, true, false};

 int[] o = longestStreak(test);
 System.out.println ("{"+o[0] + ", " + o[1]+"}");
  }



    public static int[] longestStreak(boolean[] values) {


 int  streak = 0; 
 int max =0;
 int arrsize =0;
 int maxStart = 0;
 int[] r = new int[2];


 for (int i=0; i<values.length; i++) {
   if (values[i]) {   
     streak++;
//     max =streak;
//     arrsize++;
     }

else {
    if (streak > max) {
        max = streak;
        maxStart = 0;
        maxStart = i - streak;
    }
    streak = 0;
   }
 }
 r[0] = max;
 r[1] = maxStart;
 return r;
  }


  }

应该使用这样的东西,而不是:

public static int longestStreak(boolean... values)
{
   int streak = 0; 
   int max =0;
   for (int i = 0; i < values.length; ++i)
   {
      if (values[i])
         streak++;
      else
      {
         if (streak > max)
            max = streak;
         streak = 0;
      }
   }
   return max;
}

出现此问题是因为values.length大于arrsize 考虑以下场景。
在您的输入中,有 3 个true值和 2 个false值。 您的代码表示,如果values[i]为 true,则arraysize将增加。 所以这里arraysize是 3,但values.length是布尔数组的大小,其中包含 true 和 false 也是 5。你的内循环运行到values.length即 5,你的output数组的大小是 3。这就是为什么ArrayIndexOutOfBoundsException异常发生了。

for (int i = 0; i < values.length; i++) { 
  for (int z = 1; z < values.length; z++) {
    if (values[i]) {
      i = output[z];
    }
  }
}

这里输出数组大小将小于数组大小。 因此它抛出ArrayIndexOutOfBoundsExecption

这是一个使用 ArrayList 的修改后的代码。

public static void main(String[] args) {

    boolean[] test = new boolean[] { true, false, true, true, false };

    ArrayList<Integer> o = longestStreak(test);
    for(int x : o) System.out.println(x);
}

public static ArrayList<Integer> longestStreak(boolean[] values) {
    ArrayList<Integer> maxStreak = new ArrayList<Integer>();
    int currentStreak = 0;

    for (int x = 0; x < values.length; x++)
        if(values[x]) {
            if (++currentStreak == 1)maxStreak.clear();
            if (currentStreak >= maxStreak.size()) maxStreak.add(x);

        } else currentStreak = 0;

    return maxStreak;

}

这是输出(布尔数组中最大连胜的位置)

2
3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM