1

Please consider the following code:

import java.io.*; //Sorts by dividing the array in 2 groups then joining them

public class Esercizio29 {static void  join(char[] a, int l, int m, int u) {
    char[] b = new char[u - 1 + 1];
    int i = l, j = m + 1, k = 0;

    while (i <= m && j <= u) 
        if (a[i] <= a[j])
            b[k++] = a[i++];
        else
            b[k++] = a[j++];
    while (i <= m)
        b[k++] = a[i++];
    while (j <= u)
        b[k++] = a[j++];        
    for (k = 0; k <= u - l; k++)
        a[k + l] = b[k];
}
//Sorts the array from l to u
static void sort(char[] a, int l, int u) {
    int m;
    if (l != u) {
        m = (l + u) / 2;
        sort(a,l,m);
        sort(a,m + 1,u);
        join(a,l,m,u);
    }
}



public static void main(String[] args) throws IOException{
    final int N = 16;
    char temp, v[] = new char[N];

    for (int i = 0; i <  N; i++)
        v[i] = (char) System.in.read();
    sort(v, 0, N - 1);
    System.out.println("Vettore ordinato: ");
    for(int i = 0; i < N; i++)
        System.out.print(v[i]);
    System.out.println();
}}

After running this code it gives me this result:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

at Esercizio29.fondi(Esercizio29.java:14)

at Esercizio29.ordina(Esercizio29.java:27)

at Esercizio29.ordina(Esercizio29.java:25)

at Esercizio29.ordina(Esercizio29.java:25)

at Esercizio29.ordina(Esercizio29.java:25)

at Esercizio29.main(Esercizio29.java:39)

What does this error mean and how do I solve it? thank you.

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42

3 Answers3

1

Actually java.lang.ArrayIndexOutOfBoundsException means you are accessing the array element greater than the size of the array.

example:

int[] array = {1,2,3,4};

for(int i=0;i<5;i++){
    System.out.println(array[i]);//when i =4 it will show exception.
}

because i have assigned only 4 elements to array means array size is 4. now if i want to access 5th element it will show runtime exception as array index start with 0 and array has only 4 elements.In your case at the line 14 you are access the array element greater than the array size. So it cause run time exception "java.lang.ArrayIndexOutOfBoundsException"

paardhu
  • 101
  • 8
1

Well, the ArrayIndexOutOfBoundsException error means that you attempted to access an index that is not found in your array - in this case index 1 (meaning your array contains a single value at the first (0) index).

Your exception is thrown from the join method, where you indeed try to access the 2nd index of a single sized array. You have no integrity checks on your array accessing operations buddy, which I'm afraid is a bad practice...

The following fixes will make your code run as expected:

public class Esercizio29 {static void  join(char[] a, int l, int m, int u) {
    char[] b = new char[u]; // replaced redundent [u - 1 + 1]
    int i = l, j = m + 1, k = 0;

    while (i <= m && j <= u) 
        if (a[i] <= a[j])
            b[k++] = a[i++];
        else
            b[k++] = a[j++];
    while (i <= m && k < b.length) // integrity check
        b[k++] = a[i++];
    while (j <= u && k < b.length) { // integrity check
        b[k++] = a[j++];  
    }
    for (k = 0; k <= u - l && k < a.length && k < b.length; k++) // integrity checks
        a[k + l] = b[k];
}
Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
1

Change this line of code and it will work

char[] b = new char[u + 1];

you where doing -1 + 1 which leads to ArrayIindexOutOfBounds

Rcordoval
  • 1,932
  • 2
  • 19
  • 25