1.请从键盘随机输入10个整数保存到List中,并按倒序、从大到小的顺序显示出来

@Test
public void test1() {
Scanner s = new Scanner(System.in);
System.out.println("请输入10个整数:");
List list = new ArrayList();
for (int j = 0; j < 10; j++) {
int i = s.nextInt();
list.add(i);
}
System.out.println("按照倒序的顺序显式:");
Iterator iterator = list.iterator();
Collections.reverse(list);//
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("按照从大到小的顺序显式:");
Collections.sort(list);// 默认从小到大
Collections.reverse(list);
iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}

你可能感兴趣的:(【Java】)