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

如题

//实验报告

import java.util.*;

public class TestArrayList {
	public static void main(String args[]) throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		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)");
		} //输入ArrayList
		
		System.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<String> 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);
		
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值