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

Thread: Array sorting

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Array sorting

    int array1[] = previouslyDefinedArray;
    Arrays.sort(array1);

    is this suppose to make changes also in previouslyDefinedArray? somehow it does in my project..
    i want previouslyDefinedArray to be unchanged bet somehow after sorting array1 previouslyDefinedArray is also sorted. where am I wrong?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Array sorting

    You aren't copying the array, you're copying the reference to the array. So both variables still point to the same array.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array sorting

    thank you very much, cloning did work..

  4. #4
    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: Array sorting

    cloning did work
    Please explain.
    Your code looks like there are two reference variables that point to one array.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Array sorting

    Quote Originally Posted by Norm View Post
    Please explain.
    Your code looks like there are two reference variables that point to one array.
    Yeah, I think what he's saying is that he cloned the array, copied it into the second variable, then passed that into the sort method- which worked.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Sorting an array
    By thebestpearl in forum Collections and Generics
    Replies: 5
    Last Post: April 17th, 2011, 08:58 PM
  2. [SOLVED] Selection Sorting a text array
    By ComputerSaysNo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 30th, 2011, 06:56 PM
  3. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  4. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  5. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM

Tags for this Thread