import java.math.BigDecimal; public class Test { public static void main(String[] args) { //控制随机数量 int c = 100; Number[] array = new Number[c]; /*初始化*/ for(int i=0;i<array.length;i++){ Number random = round(Math.random()*100,2); array[i] = random; } Number[] array_A = array; /* * * 乱序 * 若初始化的数组是循环的有序整形,才有打乱数组构成的必要 * */ for (int i = 0; i < array.length; i++) { Number random = Math.round(Math.random()*(c-1)); Number temp = array[i]; array[i] = array[random.intValue()] ; array[random.intValue()] = temp; } Number[] array_B = array; System.out.println(out(array_A)); System.out.println(getMidNum(array_A)); System.out.println("**"); System.out.println(out(array_B)); System.out.println(getMidNum(array_B)); } //冒泡算法 public static Number[] sort(Number[] array){ for(int i=0;i<array.length-1;i++){ //起始标和之前的结束标是个小细节 for(int j=i+1; j<array.length;j++){ Number temp; //这里可以控制正序或逆序 if(array[i].doubleValue()<array[j].doubleValue()){ temp = array[i]; array[i] = array[j]; array[j] = temp; } } } return array; } //中位数算法 public static double getMidNum(Number[] array){ array = sort(array); //java.util.Arrays.sort(array); int c = array.length; double ret = c%2==0?(array[(c/2)-1].doubleValue()+array[c/2].doubleValue())/2:array[c/2].doubleValue(); return ret; } //输出(数组之和&数组内成员) public static String out(Number[] array){ String ret = ""; double sum = 0; for (int i = 0; i < array.length; i++) { ret += array[i]; if(i!=99){ ret +=","; } sum += array[i].floatValue(); sum = round(sum,2); } return sum+":"+ret; } //四舍五入,位数 public static double round(double number,int digits){ BigDecimal b = new BigDecimal(number); return b.setScale(digits,BigDecimal.ROUND_HALF_UP).doubleValue(); } }