1

in case you're wondering, i'm not trying to simply sort an array with the lowest number first, i'm trying to sort an array by displaying the array list with the closest numbers to 0 first.

it works for some of the elements in the array but when it needs to go backwards through the array, it doesn't.. it just throws an out of bounds exception

here is my code:

package SSTF;

public class test2 {

    public static void main (String[] args)
    {
        int[] temp_array = {9, 22, 3, -4, 5, 8};



        for(int i =0; i<temp_array.length; i++)
        {   

                System.out.print(temp_array[i] + "    ");
    }

    System.out.println("\n\nNumbers closest to 0:\n");

    closestToZero(temp_array);



    }

    public static void closestToZero(int[] array) 
    {
        int num = array[0];
        int absNum = Math.abs(num);


        for(int i = 1; i < array.length; ++i) 
        {
            int newAbs = Math.abs(array[i]);

            if(newAbs < absNum) 
            {

                absNum = newAbs;
                num = array[i];

                for(int j=0; j<array.length; j++)
                {
                    System.out.print(array[i+j] + "   ");
                }
            }   
        }
    }
}

I really hope it is just something small and that someone can help, because I don't know how to fix it :S

output:

9    22    3    -4    5    8    

Numbers closest to 0:

3   -4   5   8 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at SSTF.test2.closestToZero(test2.java:42)
    at SSTF.test2.main(test2.java:18)
Conor
  • 327
  • 6
  • 15

5 Answers5

2

Here's your problem:

System.out.print(array[i+j] + "   ");

Consider the following, when:

  • i == array.length - 1 and
  • j == array.length - 1, then
  • sum of i+j is 2 * array.length - 2,

which will quite likely be out of bounds of the array.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
2

It's your use of i+j in the inner loop. I'm not entirely certain what you're trying to do there but, since both those variables can get to length-1,obviously the sum will be greater than that, hence the bounds exception.

You should probably ask yourself why you're printing out the array fresh every time you find an element closer to zero.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • yes it is I think, what i'm doing with i+j is basicaly getting it to count through the elements so that it prints out the full list(sorted), j being the count I wasn't sure how else I could display the full list sorted, closest to zero, maybe someone knows a better way – Conor Nov 21 '12 at 22:30
  • @Conor I suggest that you actually sort the array first and then print it out later with a completely different method. – Code-Apprentice Nov 21 '12 at 22:58
2

The problem is most likely with this line of code:

System.out.print(array[i+j] + "   ");

Can you guarantee that i + j is always a valid index of your array? Remember that a valid index must be between 0 and size - 1, where size is the number of elements of the array.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
2

As others have pointed out, it's the use i+j to index into the array that's causing the exception.

Now, instead of writing your own sorting routine from scratch, I would use the code from the following answer along with an appropriate comparator:

https://stackoverflow.com/a/3699501/367273

If you can use Integer[] instead of int[], you won't even need ArrayUtils, and will be able to use Arrays.sort(T[] a, Comparator<? super T> c) directly.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • ah thanks for the link, I should mention that i'm actually quite new to java and I really don't have a strong of the language yet,as of now I only have a basic understanding of it using basic objects and classes but i'll try to make sense of some of the code – Conor Nov 21 '12 at 22:38
1

i think this way is better

     int[] temp_array = {9, 22, 3, -4, 5, 8};



        for(int i =0; i< temp_array.length; i++)
        {   

                System.out.print(temp_array[i] + "    ");

        }

    System.out.println("\n\nNumbers closest to 0:\n");

Arrays.sort(temp_array);

for (int i = 0 ; i < temp_array.length; i++){

    System.out.println(temp_array[i]);

}

}
elopez
  • 109
  • 9