Conor
Conor

Reputation: 395

Sorting an array by the closest number to zero

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)

Upvotes: 1

Views: 3039

Answers (5)

elopez
elopez

Reputation: 109

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

}

}

Upvotes: 1

NPE
NPE

Reputation: 500367

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.

Upvotes: 2

sampson-chen
sampson-chen

Reputation: 47267

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.

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881523

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.

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83527

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.

Upvotes: 2

Related Questions