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.