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

Printable View

• 03-19-2012, 11:49 PM
xion0374
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:
http://i42.tinypic.com/bdpj6h.jpg

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:

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.
• 03-20-2012, 12:19 AM
doWhile
Re: How do you swap the numbers and the location of their values in an array?
• 03-20-2012, 01:01 AM
xion0374
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.