从键盘输入N(自己指定)个字符串,并使用ArrayList存储,输出ArrayList中存储的每个字符串的长度,并按照自然顺序(ASCII码)对ArrayList中存储的字符串排序。

【从键盘输入N(自己指定)个字符串,并使用ArrayList存储,输出ArrayList中存储的每个字符串的长度,并按照自然顺序(ASCII码)对ArrayList中存储的字符串排序。】如题

//实验报告import java.util.*; public class TestArrayList { public static void main(String args[]) throws Exception { ArrayList list = new ArrayList(); Scanner sc = new Scanner(System.in); System.out.println("Please start input your first element in ArrayList:(Input enter to continue)"); while(sc.hasNext()) { list.add(sc.nextLine()); System.out.println("Please input the next element:(Input Ctrl + Z to end)"); } //输入ArrayListSystem.out.println(); System.out.println("The list is"); System.out.println(list); System.out.println(); System.out.println("The number of the elements in list is: " + list.size()); //输出包含元素个数 System.out.println(); String[] sort = new String[list.size()]; for(Iterator i = list.iterator(); i.hasNext(); ) { String s = i.next(); System.out.println("The length of " + s + " is " + s.length()); }//遍历list中元素,并输出每个元素的长度Collections.sort(list); //按照元素的自然排序 System.out.println(); System.out.println("After natural sorting, the list is "); System.out.println(list); } }


    推荐阅读