本文目录一览:
- 1、java数组sort()方法降序怎么排?
- 2、java中怎样对一个整数数组进行降序排序
- 3、Java编写程序实现Comparator接口,实现字符串按 降序排序怎么弄?
- 4、Java升序降序
- 5、如何在Java中从键盘输入10个数,用选择法按降序排序并输出?
- 6、java里升序和降序最简短的代码分别是什么?
java数组sort()方法降序怎么排?
java数组sort()方法降序方式:
用Arrays类sort()对数组元素进行降序排列
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]+" ");
}
}
}
java中怎样对一个整数数组进行降序排序
int[] array = {1,2,5,8,12}; //随便定义一个数组,用冒泡排序法
for (int i = 0;i array.length;i++){
for(int j = i;j array.length;j++){
if (array[i] array[j]){
int temp = array[i];
array[i] = array[j];
array[i] = temp;
}
}
}
或者你有一个数组
List list = new ArrayList();
list.sort();
Java编写程序实现Comparator接口,实现字符串按 降序排序怎么弄?
Comparator升序降序实现 int compare(Object o1, Object o2) 返回一个基本类型的整型 1) 如果要按照升序排序, 则o1 小于o2,返回-1(负数),相等返回0,01大于02
Java升序降序
import java.util.*;
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) + "个班级成绩:");
for (int j = 0; j score.length; j++) {
System.out.print("第" + (j + 1) + "个学员的成绩:");
double s = input.nextDouble();
score[j] = s;
sum += s;
arr[i * stuNum + j] = s;
}
avgrage[i] = sum / score.length;
System.out.println("第" + (i + 1) + "个班级参赛学员的平均分是:" + 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中从键盘输入10个数,用选择法按降序排序并输出?
在Java中在数组中保存10个数,调用下面的排序函数,用选择法按降序排序:public static void selectionSort(int[] arr){ for (int i = 0; i arr.length - 1; i++) { int max = i; for (int j = i + 1; j arr.length; j++) { if (arr[max] arr[j]) { max = j; } } if (max != i) { int tmp = arr[max]; arr[max] = arr[i]; arr[i] = tmp; } } }
java里升序和降序最简短的代码分别是什么?
直接调用jdk里面的方法Arrays.sort(args)方法。而且这个方法重载实现了多个参数,排序下标[N~M]位数字~倒叙,升序等等~
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);