import?java.util.*;
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)黔西免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
public?class?Test?{
public?static?void?main(String[]?args)?{
int?stuNum?=?4;
int?classNum?=?3;
double[]?score?=?new?double[stuNum];
double?sum?=?0;
double[]?avgrage?=?new?double[classNum];
double[]?arr?=?new?double[classNum?*?stuNum];
Scanner?input?=?new?Scanner(System.?in?);
for?(int?i?=?0;?i??classNum;?i++)?{
sum?=?0;
System.out.println("請輸入第"?+?(i?+?1)?+?"個(gè)班級成績:");
for?(int?j?=?0;?j??score.length;?j++)?{
System.out.print("第"?+?(j?+?1)?+?"個(gè)學(xué)員的成績:");
double?s?=?input.nextDouble();
score[j]?=?s;
sum?+=?s;
arr[i?*?stuNum?+?j]?=?s;
}
avgrage[i]?=?sum?/?score.length;
System.out.println("第"?+?(i?+?1)?+?"個(gè)班級參賽學(xué)員的平均分是:"?+?avgrage[i]?+?"\n");
}
input.close();
System.out.println("原序:?"?+?Arrays.toString(arr));
//升序
for?(int?i?=?0;?i??arr.length;?i++)?{
for?(int?j?=?i?+?1;?j??arr.length;?j++)?{
if?(arr[i]??arr[j])?{
double?tmp?=?arr[i];
arr[i]?=?arr[j];
arr[j]?=?tmp;
}
}
}
System.out.println("升序:?"?+?Arrays.toString(arr));
//降序
for?(int?i?=?0;?i??arr.length;?i++)?{
for?(int?j?=?i?+?1;?j??arr.length;?j++)?{
if?(arr[i]??arr[j])?{
double?tmp?=?arr[i];
arr[i]?=?arr[j];
arr[j]?=?tmp;
}
}
}
System.out.println("降序:?"?+?Arrays.toString(arr));
}
}
java數(shù)組sort()方法降序方式:
用Arrays類sort()對數(shù)組元素進(jìn)行降序排列
import java.util.Arrays;
class SortDemo
{
public static void main(String[] args)
{
int[] vec={0,15,-14,45,20,70};
Arrays.sort(vec);
System.out.println("/n");
System.out.println("降序排列:");
for (int i=vec.length-1;i=0 ;i-- )
{
System.out.print(vec[i]+" ");
}
}
}
直接調(diào)用jdk里面的方法Arrays.sort(args)方法。而且這個(gè)方法重載實(shí)現(xiàn)了多個(gè)參數(shù),排序下標(biāo)[N~M]位數(shù)字~倒敘,升序等等~
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("升序:" + list);
for (int i = 0; i list.size(); i++) {
for (int j = i + 1; j list.size(); j++) {
if (list.get(i) list.get(j)) {
int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
}
System.out.println("降序:" + list);