Search code examples
javaarraysnumerical

Numbering Array items numerically


Alright, so I tried implementing the bubble sort algorithm into my code, but now my output for the second array (in my code) is giving me a ton of zeros. Can anybody tell me what is wrong with my code and how I can fix it so the zeros are removed and the only thing that remains in the output for my second array are the fixed numerically?

public static void main(String[] args) {

        System.out.println("Input up to '10' numbers for current array: ");

        int[] array1 = new int[10];
        int i;

        Scanner scan = new Scanner(System.in);

        for (i = 0; i < 10; i++) {
            System.out.println("Input a number for " + (i + 1) + ": ");
            int input = scan.nextInt();
            if (input == -9000) {
                break;
            } else {
                array1[i] = input;
            }
        }

        System.out.println("\n" + "Original Array: ");

        for (int j = 0; j < i; j++) {

            System.out.println((j + 1) + ": " + array1[j]);
        }

        int[] array2 = new int[i];

        System.out.println("\n" + "Organized Array: ");

        for (int j = 0; j < i; j++) {

            int temp;
            boolean organized = false;

            while (organized == false) {
                organized = true;


            for (i = 0; i < array1.length - 1; i++) {

                if (array1[i] > array1[i + 1]) {
                    temp = array1[i + 1];
                    array1[i + 1] = array1[i];
                    array1[i] = temp;
                    organized = false;
                }
            }

            }
            for (i = 0; i < array1.length; i++) {
                System.out.println(array1[i]);
            }
            scan.close();

        }
    }
}

Solution

  • Copy your array1 to an array2 of the correct length before sorting, something like

    for (i = 0; i < 10; i++) {
        System.out.println("Input a number for " + (i + 1) + ": ");
        int input = scan.nextInt();
        if (input == -9000) {
            break;
        }
        array1[i] = input;
    }
    int[] array2 = Arrays.copyOfRange(array1, 0, i);
    System.out.println("Before sorting: " + Arrays.toString(array2));
    Arrays.sort(array2); // <-- How I would sort.
    System.out.println("After sorting: " + Arrays.toString(array2));
    

    The reason this is necessary is because i might not be 10 in which case your array contains 0(s) to fill the other positions.

    Is it possible to move all my numbers from Array 1 to Array 2 using a for-loop?

    Yes. You could implement a copyOfRange function with a for loop,

    private static int[] copyOfRange(int[] arr, int start, int end) {
        int pos = 0;
        int[] out = new int[end - start];
        for (int i = start; i < end; i++) {
            out[pos] = arr[i];
            pos++;
        }
        return out;
    }
    

    the built-in version is almost certainly better.