java字符串转字符串列表_Java中的字符串列表示例

java字符串转字符串列表

We have to read total number string i.e. "n", create a list of the Strings and input "n" strings, add then to print the all stings of the list in Java.

我们必须读取总数字符串,即“ n”,创建字符串列表并输入“ n”字符串,然后添加以在Java中打印列表的所有字符串。

Declare a list of String implemented by ArrayList:

声明由ArrayList实现的String列表:

    List<String> str_list = new ArrayList<String>();

To input the total number of strings and to input string from the user, we are using in which is an object of scanner class.

到串并从用户输入字符串的总数,我们使用其中输入是扫描器类的对象。

The statement to read string and add to the list in the loop is:

读取字符串并添加到循环列表中的语句是:

    str_list.add(in.next());

Program:

程序:

import java.util.*;

public class ListExample {
	public static void main (String[] args) {
		//creating a list of integers
		List<String> str_list = new ArrayList<String>();
		int n;
		Scanner in = new Scanner (System.in);
		System.out.print ("Enter total number of strings: ");
		n = in.nextInt ();

		for (int i=0; i<n; i++){
			System.out.print ("Enter name "+(i+1) +": ");
			str_list.add(in.next ()) ;
		}

		//printing updated List
		System.out.println ("string list: " + str_list);
		//printing elements
		System.out.println ("List elements: ");
		for(String str: str_list){
			System.out.println (str); 
		}
	}
};

Output

输出量

Enter total number of strings: 5
Enter name 1: Delhi
Enter name 2: Noida
Enter name 3: Mumbai
Enter name 4: Gwalior
Enter name 5: Bhind
string list: [Delhi, Noida, Mumbai, Gwalior, Bhind]
List elements:
Delhi
Noida
Mumbai
Gwalior
Bhind


翻译自: https://www.includehelp.com/java-programs/list-of-strings-example-in-java.aspx

java字符串转字符串列表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值