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: Need helpt with sorting arrays?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need helpt with sorting arrays?

    Hey I'm having some issue with my array list. What I want to do is not do a complete sorting on the array but just swapping the position of 2 numbers in the array list without sorting the entire array. I want to swap the place of the largest number to the place of the smallest number in the array list, but I'm stuck and I don't know what the right code to use to do this. I know how to sort and all but swapping without fully sorting is a little different.

    Here is the example result:


    As you can see in the image there, the #86 at the 8th spot in the element is swap with the #19 at the 4th spot. This is what I am trying to get in my program.

    Here is my code so far, im kinda stuck, and need to know what code I need to add.

    1. I need to add the code to the System.out.println line statement to show the number value + the position of the number in the array list.
    2. To reproduce the array list again with the new placement of the largest and smallest numbers.
    3. Keep in mind that the array has random generated numbers, so using the temp in place to hold the values may not work.

    Here is my code:

    public class SwapMaxMin {
    	public static void main(String[] args){
     
    		//declaring an integer with an array of 10 elements.
    		int[] mySwap = new int[10];
    		int i;
     
    		//initialized an array with random number
    		for (i = 0; i < mySwap.length; i++)
    			mySwap[i] = (int)(Math.random()*99);
    		System.out.println("Before swapping:");
     
    		//printing the array
    		for (i = 0; i < mySwap.length; i++)
    			System.out.println("myScore[" + i + "]" + mySwap[i]);
     
                    //Im stuck here trying to figure out what java code i need to add to the system.out.println
    		System.out.println("The largest number is " + mySwap[i] + " and at [ " + + " ]");  //<-----
    		System.out.println("The Smallest number is " + mySwap[i] + " and at [ " + + " ]");  //<-----
     
     
                    //Then how do I go about regenerating the same array list again with the addition of the change in the largest 
                    //and smallest number place.  The array should stay the same except for the swap of those 2 numbers.
    		System.out.println("After swapping");  //<-----
     
     
    		System.out.println("Done!");
    	}
    }

    Awaiting feed back, thanks.
    Last edited by xion0374; March 19th, 2012 at 05:07 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need helpt with sorting arrays?

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/56966-how-do-you-swap-numbers-location-their-values-array.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need helpt with sorting arrays?

    thanks for the heads up, I did not know and I have search to find my answer but I wasn't able to, therefore I posted on more than one site, and did not realize that there are rules against it. I'll keep that in mind for future reference, but pertain to my post I am still trying to figure out the codes and still waiting for help.

Similar Threads

  1. Sorting Arrays
    By Bryan29 in forum Collections and Generics
    Replies: 5
    Last Post: November 28th, 2011, 07:21 AM
  2. Digital Watch program and Sorting arrays
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 05:21 PM
  3. [SOLVED] Sorting and Merging
    By javapenguin in forum What's Wrong With My Code?
    Replies: 36
    Last Post: December 7th, 2010, 08:04 PM
  4. [SOLVED] sorting
    By kite98765 in forum Algorithms & Recursion
    Replies: 8
    Last Post: February 4th, 2010, 08:34 AM
  5. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM