java 中,如何将1~100的偶数所组成的List集合倒序排列,谢谢!
展开全部
public class SortNums {
public static void main(String[] s){
List<Integer> list = new ArrayList<Integer>();
for(int i=1;i<=100;i++){
if(i%2==0){
list.add(i);
}
}
System.out.println(list);
Collections.sort(list,Collections.reverseOrder());
System.out.println(list);
}
}
好简单的,输出结果为:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
[100, 98, 96, 94, 92, 90, 88, 86, 84, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
public static void main(String[] s){
List<Integer> list = new ArrayList<Integer>();
for(int i=1;i<=100;i++){
if(i%2==0){
list.add(i);
}
}
System.out.println(list);
Collections.sort(list,Collections.reverseOrder());
System.out.println(list);
}
}
好简单的,输出结果为:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]
[100, 98, 96, 94, 92, 90, 88, 86, 84, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
展开全部
用 java.util.Collections.sort(java.util.List) 试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
还有种求偶的方法( x & 1 ) == 0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class SortNums {
public static void main(String[] s){ List<Integer> list = new ArrayList<Integer>(); for(int i=100;i>=1;i--){ if(i%2==0){ list.add(i); } } System.out.println(list); }}
public static void main(String[] s){ List<Integer> list = new ArrayList<Integer>(); for(int i=100;i>=1;i--){ if(i%2==0){ list.add(i); } } System.out.println(list); }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询