创建数字集合后的排序方法

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

 

public class Test3 {

 public static void main(String[] args) {

  Random ran = new Random();

  List list = new ArrayList();

  while (true) {

   int n = ran.nextInt(100);

   if (!list.contains(n)) {
    list.add(n);
   }
   if (list.size() == 20) {
    break;
   }
  }

  // 排序
  
  System.out.println(list);
  
  Collections.sort(list);
  
  Collections.reverse(list);//倒序
  
  System.out.println(list);
  
  System.out.println(Collections.max(list));//求最大
  System.out.println(Collections.min(list));//求最小

 }
}

打印结果:

[15, 76, 2, 19, 48, 83, 51, 31, 82, 6, 11, 36, 57, 33, 42, 38, 69, 7, 70, 91]
[91, 83, 82, 76, 70, 69, 57, 51, 48, 42, 38, 36, 33, 31, 19, 15, 11, 7, 6, 2]
91
2

你可能感兴趣的:(list,nextint)