【问题标题】:Return values of array from index to n?将数组的值从索引返回到 n?
【发布时间】:2014-03-07 04:45:54
【问题描述】:

我只想创建一个方法,该方法将返回从索引开始到索引 + n 的数组值。例如 - 索引 3 和 n 的 4 应该返回值

//Array[3]
//Array[4]
//Array[5]
//Array[6]
//Array[7]

public int[] subSequence(int index, int n, int[] array) {
int[] valuelist = new int[array.length];
    for(int i = index; i <= n + index; i++)
    {
        if((index + n) <= array.length)
        valuelist[i] = array[i];
    }
   return valuelist;

}

我很抱歉发布这样愚蠢的问题,但我是新手,找不到类似的问题。

我的问题:我正在为我正在做的事情收到ArrayIndexOutOfBounds,但我真的不知道如何解决这个问题。

【问题讨论】:

  • 循环中的 a1、a2 是什么?
  • 什么是a1a2?我在您的代码中的任何地方都看不到它们。但是,如果您收到的是ArrayIndexOutOfBoundsException,它的含义正是它看起来的含义;在某些时候,i 要么是负数,要么是 &gt;= valuelist.length&gt;= array.length;所以在你的脑海中仔细研究它并寻找发生这种情况的机会。
  • 感谢您指出这一点。我编辑了它

标签: java arrays indexoutofboundsexception


【解决方案1】:

这可能对你有帮助,

   for(int i = n; i <=( n + index); i++)
    {

         if( i >=array.length){//check length of array with index,n

          break;
         }
     else{
        valuelist[i] = array[i];
        }
    }

【讨论】:

  • 请注意,&lt;= 仍然是在该循环中执行的错误测试。
  • 如果index = 5 n = 10 在一个大小为 3 的数组中怎么办。idexOutOfBoundException 再次出现~
【解决方案2】:
  1. Google for ArrayIndexOutOfBounds
  2. 阅读第一个链接(异常的JavaDoc:http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html

抛出表明一个数组被非法访问 指数。指数为负或大于或等于 数组的大小。

【讨论】:

  • 是的。这是一个相当无用的答案,更适合对问题发表评论。
  • OP 没有证明他们在发布之前已经对问题的原因进行了任何研究。阅读对异常的解释应该提供足够的信息来解决问题而无需询问。
  • 是的,这可能是正确的; 并且这是一个相当无用的答案,更适合对问题发表评论。有关更具建设性的答案的示例,请参阅此处的其他一些回复。 :)
  • 什么,你的意思是接受的答案只是正确的代码,没有解释什么问题是什么?这对 OP 的学习过程帮助更小……
  • 我同样感到失望的是,这是公认的答案。我曾希望它会有所改进,因为这是一个好的开始。
【解决方案3】:

你有:

for(int i = index; i <= n + index; i++)

你的意思可能是:

for(int i = index; i < n + index; i++)

我假设index 是开始索引,n 应该是计数。在您当前的实现中,使用&lt;=,请考虑以下情况:

int[] a = new int[50];
int[] b = subSequence(0, 50, a);

您可以想象这会起作用,但您的循环将持续到i &lt;= 0+50,这意味着在某个点i==50,它超出了数组的末尾(数组中的最后一个索引是49)。

您遇到的另一个问题是:

if((index + n) <= array.length)
    valuelist[i] = array[i];

这看起来您正试图将索引保持在界限内。更仔细地检查 if 语句,因为它不是这样做的。

【讨论】:

    【解决方案4】:

    if((index + n) &lt;= array.length)这行代码就是java.lang.ArrayIndexOutOfBoundsException的问题。

    代码表示要执行 if 语句中的语句 if index + n 小于或等于数组的长度。现在,当index+n 等于数组的长度时,在此语句valuelist[i] = array[i]; 中尝试为数组设置值时将出现 ArrayIndexOutOfBoundsException

    要避免 ArrayIndexOutOfBoundsException 试试这个:

    if((index + n) &lt; array.length)

    此代码删除了等于条件,仅当表达式index + n 小于数组长度时才会执行。 :D

    要记住的重要一点是,数组在访问时从零开始。数组的长度属性不是从零开始的。因此,如果代码尝试使用长度属性访问数组元素,它将抛出此运行时异常 ArrayIndexOutOfBoundsException。这是因为代码试图访问一个元素,一个元素通过了数组的末尾。

    例子:

    int [] x = {0, 1}; // x.length = 2 and 1 is at position 1 and 0 is at position 0
    System.out.println(x[x.length]); //throws ArrayIndexOutOfBoundsException
    System.out.println(x[0]); // prints the first element 0
    

    快乐编码。

    【讨论】:

      【解决方案5】:

      如果您不知道数组的大小,最好使用 arraylist 或其他一些固定大小的集合类。之后,您将其转换为数组或按原样使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-08
        • 2018-11-14
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        相关资源
        最近更新 更多