1 public class Demo { 2 public static void main(String[] args) { 3 List list = Arrays.asList(5,2,3,1,9); 4 int N = list.size(); 5 int t = 0; 6 Collections.sort(list); 7 // Collections.reverse(list); 逆序 8 // Collections.shuffle(list); 随机 9 boolean flag = true; 10 for(int i=0; i<list.size()-1 ; i++){ 11 int a = (int)list.get(i+1); 12 int b = (int)list.get(i); 13 int c = a - b; 14 if(c != 1){ 15 flag = false; 16 System.out.print("数字不连续哦"); 17 break; 18 } 19 } 20 System.out.println(flag); 21 } 22 }
Collections类里好多方法,具体,自己读API吧