Results 1 to 3 of 3
  1. #1
    xion0374 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default How do you swap the numbers and the location of their values in an array?

    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.

    Here is my code:

    Java 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.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: How do you swap the numbers and the location of their values in an array?


  3. #3
    xion0374 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: How do you swap the numbers and the location of their values in an array?

    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. Swap 2 dimensional Array
    By whit3ang3l in forum New To Java
    Replies: 4
    Last Post: 03-06-2012, 12:53 PM
  2. Swap array value
    By irnie1994 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 05:06 PM
  3. how can i loop my array swap
    By belfast09 in forum New To Java
    Replies: 3
    Last Post: 06-15-2011, 04:35 AM
  4. Multidimensional array - swap
    By BeginnerNoob in forum New To Java
    Replies: 13
    Last Post: 03-30-2011, 03:02 PM
  5. how to swap 2 numbers
    By mary in forum Advanced Java
    Replies: 1
    Last Post: 08-02-2007, 05:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •