简体   繁体   中英

Java sorting program, getting a confusing output

public static void main(String[] args) throws Exception {
    // declarations
    int i, z, x, greatest;
    int[] array = { 2, 3, 4, 55, 6 };
    int[] copyarray = { 0, 0, 0, 0, 0 };
    int zz;
    greatest = array[0];

    for (zz = 0; zz < 5; zz++) {
        for (x = 0; x < 5; x++) {
            if (array[x] > greatest) {
                greatest = array[x];
            }
        }

        copyarray[zz] = greatest; // this will contain the sorted array
        // part of the nested loop
        for (z = 0; z < 5; z++) {
            if (greatest == array[z]) {
                array[z] = 0;
            }

        }
    }
    // not part of the nested loop
    for (i = 0; i < 5; i++) {
        System.out.println("sorted array: " + copyarray);
    }
}

Output:

sorted array: [I@1a16869
sorted array: [I@1a16869
sorted array: [I@1a16869
sorted array: [I@1a16869
sorted array: [I@1a16869

This is just a basic little program and I'm trying to get the logic right. I can't improve it or make it into a class or method because I'm not even getting the output right.

If you are trying to use your own algorithm, i would suggest you try using IDE and debug the code.

If you want to use algorithm that JDK provides, you could use:

Arrays.sort(array);

Regarding the output, you are trying to print array and array is an object without toString implementation in java. Hence you should change your print statement to :

System.out.println("sorted array: "+Arrays.toString(copyarray));//without surrounding for loop to get what its after each step of sorting elements.

Or if you want to keep your for loop then you could use index based access to array like:

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

You are printing the reference not the value use:

for(int i = 0; i < copyarray.length; i++ ) {
    System.out.println("Value : " + copyarray[i]);
}

i would also recommend using Arrays.sort(array);

just write

private int[] values = { 9,2,5,3,1,7,0 };

  public void printSorted() {
    Arrays.sort(values);
    for(int i = 0; i < values.length; i++) {
        System.out.println("Value: " + values[i]);
    }
  }

Actually, none of the answers here are right.

The heart of the problem is that you are not re-initializing the variable greatest for each iteration. It is set to array[0]; in the beginning and it is never changed again. This should go inside the loop.

public static void main(String[] args) throws Exception {
    // declarations
    int i, z, x, greatest;
    int[] array = { 2, 3, 4, 55, 6 };
    int[] copyarray = { 0, 0, 0, 0, 0 };
    int zz;
    // greatest = array[0]; <---- Don't do it here

    for (zz = 0; zz < 5; zz++) {
        greatest = array[0]; // <--- Initialize greatest here and not before the loop
        for (x = 0; x < 5; x++) {
            if (array[x] > greatest) {
                greatest = array[x];
            }
        }

        copyarray[zz] = greatest; // this will contain the sorted array
        // part of the nested loop
        for (z = 0; z < 5; z++) {
            if (greatest == array[z]) {
                array[z] = 0;
            }

        }
    }
    // not part of the nested loop
    for (i = 0; i < 5; i++) {
        System.out.println("sorted array: " + copyarray[i]);
    }
}

As a side note, you are printing the array incorrectly, you should use copyarray[i] and not copyarray .

Whit these two changes, here's the output:

sorted array: 55
sorted array: 6
sorted array: 4
sorted array: 3
sorted array: 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM