Java :输入一整组存放在数组中的,比较并输出其中的最大值和最小值,再将数组元素从小到大排序并输出

要能运行的出来哟!

第1个回答  推荐于2016-05-30
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
/**
* 数组
* @作者 胡楠启
*
*/
public class Test2 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入整数数组的长度:");
int len=sc.nextInt();
System.out.println("请输入整数数组:");
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=0;i<len;i++){
list.add(sc.nextInt());
}
//先排序
Collections.sort(list);
//输出整个数组
System.out.println("数组的值为:");
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("数组最大值为:"+list.get(len-1));
System.out.println("数组最小值为:"+list.get(0));
}

}本回答被提问者采纳