Problem with Random Numbers
I'm working on this program where I create an array of random integers and use bubble sort to get them in order:
Code :
package bubblesort;
import java.util.Random;
public class BubbleSort {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
final int size = 10;
int[] array = new int[size];
Random randomNumber = new Random();
for(int i=0; i<array.length; i++){
array[i] = randomNumber.nextInt(29);
}
int[] sortedArray = bubbleSort(array);
System.out.println(sortedArray);
}
static int[] bubbleSort(int[] numbers){
int n = numbers.length;
for(int pass=1; pass<=n; pass++){
for(int current=0; current<n-pass; current++){
if(numbers[current] > numbers[current+1]){
//swap
int temp = numbers[current];
numbers[current] = numbers[current+1];
numbers[current+1] = temp;
}
}
}
return numbers;
}
}
instead of it outputting the sorted numbers I get an output of: [I@4669b7fe
any thoughts?
Re: Problem with Random Numbers
Quote:
output of: [I@4669b7fe
That is the output from the toString() method for a int array. If you want to format an array for printing, use this:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
Re: Problem with Random Numbers
Thanks. That helped a ton