Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with Random Numbers

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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:

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with Random Numbers

    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:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    3sbwya (February 17th, 2013)

  4. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Random Numbers

    Thanks. That helped a ton

Similar Threads

  1. Can't get seven random numbers, only one.
    By alpvii in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 6th, 2010, 05:39 PM
  2. random numbers in array please help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 01:54 AM
  3. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  4. help me .. about creating random numbers
    By soldier in forum Java Theory & Questions
    Replies: 2
    Last Post: December 20th, 2009, 08:24 PM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM