Вопрос

I just want to make a method which would return the values of the array starting from index going till index + n. For example - An index of 3 and n of 4 should return the values

//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;

}

I am sorry for posting such dumb question but I am new to this and couldn't find a similar question.

My problem : I am getting ArrayIndexOutOfBounds for what I am doing and I don't really have an idea about how this can be solved.

Это было полезно?

Решение

This may help you,

   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];
        }
    }

Другие советы

  1. Google for ArrayIndexOutOfBounds.
  2. Read the first link (the JavaDoc for the exception: http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html)

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

You have:

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

You probably mean:

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

I'm assuming index is the start index and n is supposed to be the count. In your current implementation, with <=, consider the case:

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

You would imagine that would work, but your loop would go as long as i <= 0+50, which means at some point i==50, which is beyond the end of the array (the last index in the array is 49).

Another side-issue you have is:

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

This looks like you're attempting to keep the index in bounds. Examine that if statement a little more closely, because that is not what it is doing.

if((index + n) <= array.length) this line of code is the problem with the java.lang.ArrayIndexOutOfBoundsException.

The code says to execute the statements inside the if statement if index + n is less than or equal to the length of the array. Now, when index+n is equal to the length of the array there will be an ArrayIndexOutOfBoundsException when trying to set values for your arrays in this statement valuelist[i] = array[i];

To avoid the ArrayIndexOutOfBoundsException try this:

if((index + n) < array.length)

This code has the equals condition removed and will only execute if the expression index + n is less than the length of the array. :D

An important point to remember is that arrays begin at zero when accessed. The length attribute of array is not zero based though. So if the code tries to access an array element using the length attribute it will throw this runtime exception, ArrayIndexOutOfBoundsException. This is because the code tries to access an element one element passed the end of the array.

Example:

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

Happy Coding.

if you don't know the size of array better use arraylist or some other collection classes with fixed size. After that you convert it to array or use it as it is.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top