黑马程序员技术交流社区

标题: 删除一维数组中的重复数 [打印本页]

作者: 沙漠中的狼    时间: 2016-4-27 17:03
标题: 删除一维数组中的重复数
class Day05_Work3 {
public static void main(String[] args) {
  int[] arr = {6,2,9,15,1,5,20,7,18};
  int a = getMax(arr);
  int b = getMin(arr);
  System.out.println(a);
  System.out.println(b);
  array(arr,a,b);
}
public static int getMax(int[] arr) {
  int temp = arr[0];
  for (int a = 1;a < arr.length ;a++ ) {
   if (temp <=  arr[a] ) {
    temp = arr[a];
   }
  }
  int sum1 = 0;
  for (int b =0;b < arr.length ;b++ ) {
   if (arr[b] == temp) {
    break;
   }
   sum1++;
  }
  return sum1;
}
public static int getMin(int[] arr) {
  int temp = arr[0];
  for (int a = 1;a < arr.length ;a++ ) {
   if (temp >= arr[a] ) {
    temp = arr[a];
   }
  }
  int sum1 = 0;
  for (int b =0;b < arr.length ;b++ ) {
   if (arr[b] == temp) {
    break;
   }
   sum1++;
  }
  return sum1;
}
public static void array(int[]arr ,int a, int b) {
  
   int temp = 0;
   temp = arr[0];
   arr[0] = arr[b];
   arr[b] = temp;
   
   int temp1 = 0;
   temp1 = arr[arr.length-1];
   arr[arr.length - 1] = arr[a];
   arr[a] = temp1;
   for (int c = 0;c < arr.length ;c++ ) {
    System.out.print(arr[c]+ " ");
   }
}
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2