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 6 of 6

Thread: Sort method not getting the right value back.

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Sort method not getting the right value back.

    Hey.

    I have this method that, when used, should put order to the array that i have and then i should be able to call it from main and print it from there. However i cannot seem to get anything right out from it.

    I always get "[I@7d4991ad" as a print back..

    I am calling it like this in my main:
    System.out.println(sortt(arr));

    And this is the method:
    private static int[] sortt(int[] arr) {
    		int toSwap, indexOfSmallest = 0;
    		int i, j, smallest;
     
    		for (i = 0; i < arr.length; i++) {
     
    			smallest = Integer.MAX_VALUE;
     
    			for (j = i; j < arr.length; j++) {
    				if (arr[j] < smallest) {
    					smallest = arr[j];
    					indexOfSmallest = j;
    				}
    			}
     
    			toSwap = arr[i];
    			arr[i] = smallest;
    			arr[indexOfSmallest] = toSwap;
    		}
     
    		return arr;
     
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,316
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Sort method not getting the right value back.

    i cannot seem to get anything right out from it.
    To print the contents of an array use the Arrays class's toString method:
       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. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Sort method not getting the right value back.

    The thing is that i am not allowed to use the array lib. :/

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Sort method not getting the right value back.

    Then write your own. Remember how you built the String in an earlier exercise for your tostring method.

    Regards,
    Jim

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,316
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Sort method not getting the right value back.

    Then you will have to write your own method to display the contents of an array.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Sort method not getting the right value back.

    Quote Originally Posted by Norm View Post
    Then you will have to write your own method to display the contents of an array.
    Quote Originally Posted by jim829 View Post
    Then write your own. Remember how you built the String in an earlier exercise for your tostring method.

    Regards,
    Jim
    Thanks guys!

Similar Threads

  1. Sort String Method
    By KajuMax in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 7th, 2014, 09:36 PM
  2. Replies: 1
    Last Post: May 27th, 2014, 07:39 PM
  3. Replies: 2
    Last Post: May 27th, 2014, 12:36 PM
  4. Replies: 2
    Last Post: November 11th, 2012, 09:44 PM
  5. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 02:27 PM