I'm supposed to make a static method named swap() that scans an array of integers that takes in the min and max and swaps them. So if max has an index of 0 and min index of 3, index[5] = [3] now and index [3] = [5] now. So the array is created by user inputs:
public static int[] readInputs(int arraySize) {
System.out.print("What length is the array? ");
arraySize = console.nextInt();
int[] intArray = new int[arraySize];
for (int i = 0; i <= intArray.length - 1; i++) {
System.out.print("Enter an integer: ");
userInput = console.nextInt();
intArray[i] = userInput;
}
return intArray;
} //end of readInputs()
I am supposed to make another static method that swaps the min and max called swap():
public static int[] swap(int[] intArray){
for(int index = 0; index < intArray.length; index++){
if (intArray[index] > max){
max = intArray[index];
}
if (intArray[index]<min) {
min = intArray[index];
}
}
} //end of swap()
So far, I have it so it finds the min and max, but I'm stuck on how to swap.
maxandmin, save the index of the maximum and minimum value.max = intArray[index];is the actual element. If you want to save the index,max = index;(and of course max is andint)