package com.companyname.common.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; /** * @Description 键盘录入多个数据,以0结束,并在控制台输出最大值 * @Author Created by shusheng * @Email shusheng@yiji.com * @Date 2018/12/14 */ public class ArrayListDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<Integer> arrayList = new ArrayList<Integer>(); while(true){ System.out.println("请输入数据:"); int number = scan.nextInt(); if(number!=0){ arrayList.add(number); }else{ break; } } Integer[] i = new Integer[arrayList.size()]; arrayList.toArray(i); Arrays.sort(i); System.out.println("数组是:" + arrayToString(i) + ",最大值是:"+ i[i.length - 1]); } private static String arrayToString(Integer[] i) { StringBuilder sb = new StringBuilder(); sb.append("["); for(int x=0;x<i.length;x++){ if(x==i.length-1){ sb.append(i[x]); }else{ sb.append(i[x]+","); } } sb.append("]"); return sb.toString(); } }