Results 1 to 3 of 3
- 03-19-2012, 10:49 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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:
Awaiting feed back, thanks.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!"); } }
- 03-19-2012, 11:19 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: How do you swap the numbers and the location of their values in an array?
Cross posted: Need helpt with sorting arrays?
- 03-20-2012, 12:01 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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
-
Swap 2 dimensional Array
By whit3ang3l in forum New To JavaReplies: 4Last Post: 03-06-2012, 12:53 PM -
Swap array value
By irnie1994 in forum New To JavaReplies: 1Last Post: 11-10-2011, 05:06 PM -
how can i loop my array swap
By belfast09 in forum New To JavaReplies: 3Last Post: 06-15-2011, 04:35 AM -
Multidimensional array - swap
By BeginnerNoob in forum New To JavaReplies: 13Last Post: 03-30-2011, 03:02 PM -
how to swap 2 numbers
By mary in forum Advanced JavaReplies: 1Last Post: 08-02-2007, 05:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks