0

I have been working on this code for about a week; I am trying to make a program that generate random numbers and then sort them using the Bubble method, but I get this message "Bubble Sort: [I@ad3ba4". Does anyone see what is wrong I feel like this is so simple but I just can't find the problem.

import java.util.Random;
public class sortLibrary {
    private static void bubbleSort(int[] list) {
        int n = list.length;
        int temp = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - 1); j++) {
                if (list[j - 1] > list[j - 1]) {
                    temp = list[j - 1];
                    list[j - 1] = list[j];
                    list[j] = temp;
                }
            }
        }
        System.out.println("\nBubble Sort: " + list);
    }
    public static void main(String args[]) {
        System.out.println("Unsorted list:");
        Random numbers = new Random();
        int list[] = new int[20];
        for (int i = 0; i < 20; i++) {
            list[i] = numbers.nextInt(100);
        }
        for (int i = 0; i < 20; i++) {
            System.out.print(list[i] + "  ");
        }
        bubbleSort(list);
    }
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89

3 Answers3

3

You cannot simply print a list with a println(). That is only for strings.

Instead, replace list in your println() with Arrays.toString(list) to convert the array to a string for printing.


Or, you can print it out as an array, similar to @KyleGowen's answer. Here's an alternate form of a way to iterate over an array in Java easier:

String arrayToString = "";
for(int item : list) {
    arrayToString += item + ", ";
}
System.out.println("Bubble sort: [" + arrayToString.substring(0, arrayToString.length()-2) + "]");

This should also print it nicely.

See both of these at TutorialPoint's online Java compiler.

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
1

Now, you can not print Array in java just like that. If you want to print all array you need to use index or use something like this:

System.out.println("\nBubble Sort: " + Arrays.toString(list));

Also in your if statement you have list[j - 1] > list[j-1]. I think there will be list[j - 1] > list[j]. So your code looks like:

import java.util.Arrays;
import java.util.Random;

public class bubble {

    private static void bubbleSort(int[] list) {
        int n = list.length;
        int temp = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - 1); j++) {

                if (list[j - 1] > list[j]) {
                    temp = list[j - 1];
                    list[j - 1] = list[j];
                    list[j] = temp;
                }
            }
        }
        System.out.println("\nBubble Sort: " + Arrays.toString(list));
    }

    public static void main(String args[]) {
        System.out.println("Unsorted list:");
        Random numbers = new Random();
        int list[] = new int[20];
        for (int i = 0; i < 20; i++) {
            list[i] = numbers.nextInt(100);
        }
        for (int i = 0; i < 20; i++) {
            System.out.print(list[i] + "  ");
        }

        bubbleSort(list);

    }
}
LEQADA
  • 1,913
  • 3
  • 22
  • 41
0

You are actually printing out the memory address of your list with

System.out.println("\nBubble Sort: "+list);

You will most likely want to loop through the list and print out the values. Something like:

System.out.print("\nBubble Sort: ");
for(int i = 0; i < list.length; i++){
    System.out.print(list[i] + " ");
}

I hope this helps.

Kyle Gowen
  • 467
  • 2
  • 8