- class Test {
- public static void main(String[] args) {
- int[] arr = new int[]{6,2,9,15,1,5,20,7,18};
- print(arr);
- System.out.println();
- int maxMark = getMaxMark(arr);
- System.out.println(maxMark);
-
- int minMark = getMinMark(arr);
- System.out.println(minMark);
-
- revArray(arr,maxMark);
- rArray(arr,minMark);
- print(arr);
- }
- public static void print(int[] arr) {
- for (int i = 0;i < arr.length ;i++ ) {
- System.out.print(arr[i] + ",");
- }
- }
- public static int getMaxMark(int[] arr) {
- int mark = arr[0];
- for (int i = 0;i < arr.length ;i++ ) {
- if (arr[mark] < arr[i]) {
- mark = i;
- }
- }
- return mark;
- }
- public static int getMinMark(int[] arr) {
- int min = arr[0];
- for (int i = 0;i < arr.length ;i++ ) {
- if (arr[min] > arr[i]) {
- min = i;
- }
- }
- return min;
- }
- public static void revArray(int[] arr,int maxMark) {
- int temp = 0;
- temp = arr[arr.length - 1];
- arr[arr.length - 1] = arr[maxMark];
- arr[maxMark] = temp;
- }
- public static void rArray(int[] arr,int minMark) {
- int temp = 0;
- temp = arr[0];
- arr[0] = arr[minMark];
- arr[minMark] = temp;
- }
- }
复制代码 |