-2

Hey I'm trying to test my selection sort algorithm but all the output I get in the console is just "[I@15db9742" Could someone please explain to me why I'm getting junk output? It is really baffling me, could it possible be a problem with the IDE or is it something in the code?

Thanks

import java.util.Arrays;


public class SelectionSorterTest {

    // Factories

    // Queries

    /**
     * Sorts a copy of the input array.
     * @param input an array.
     * @return a sorted copy of input.
     */
    public static int[] sort (int[] input) {
        int[] sorted = Arrays.copyOf(input, input.length);
        for(int i = 0; i < sorted.length - 1; i++)
        {
            int minIndex = i;
            for(int j = i + 1; j < sorted.length - 1; j++)
            {
                if(sorted[j] < sorted[minIndex])
                {
                    minIndex = j;
                }
            }
            if(i != minIndex)
            {
                swap(sorted, minIndex, i);
            }
        }
        return sorted;
    }


    // Commands

    // Private

    // disabled constructor
    private SelectionSorterTest () { }

    private static void swap (int[] arr, int x, int y) {
        int temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
    }

    public static void main(String[] args)
    {
        int[] Array = {9,7,6,4,3,2,21,1};
        System.out.println(sort(Array));

    }

}

4 Answers4

0

You are printing array object directly on console.

Use java.util.Arrays.toString(int[]) method. It returns a string representation of the contents of the specified int array.

In your case it would be System.out.println(Arrays.toString(sort(Array)));

T-Bag
  • 10,916
  • 3
  • 54
  • 118
0

I@15db9742 is the the result of calling long[].toString(), consisting of the type signature of long[] plus an '@' plus the hashCode() (thank you EJP)

Access the element in the array to print it.

public static void main(String[] args)
{
    int[] Array = {9,7,6,4,3,2,21,1};
    for(int n : sort(Array)){
        System.out.println(n);
    }
}
OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
  • It is the result of calling `long[].toString()`, consisting of the type signature of `long[]` plus an '@' plus the `hashCode()`. Not a memory address. – user207421 Aug 09 '17 at 04:25
0

By using this line System.out.println(sort(Array)); you are printing out an array's address in memory. Use a loop to print the element of that array! By the way, your algorithm is incorrect because you are missing the last element in the for loop. Remove the -1 part out of the loop to correct it. Thanks

TuyenNTA
  • 1,194
  • 1
  • 11
  • 19
0

The problem is that you print the array directly to the console:

  • you need to use Arrays.toString(anArray) or your custom function:

In case of using Arrays.toString(anArray):

  • First you need to import the Arrays class to our program:

    import java.util.Arrays;
    
  • Second Then change your print statement from:

    System.out.println(sort(Array));
    

    To be

    System.out.println(Arrays.toString(sort(Array)));
    

In case of your custom method:

  • Implement your own function like this one, just to exclude the brackets:

    private static void printArray(int[] anArray) {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < anArray.length; i++) {
         if (i > 0) {
            sb.append(", ");
         }
         sb.append(anArray[i]);
      }
      System.out.println(sb.toString());
    

    }

  • And change your print statement to be:

    System.out.println(printArray(sort(Array)));
    
Fady Saad
  • 1,169
  • 8
  • 13