简体   繁体   English

如何从单个jtextfield的输入创建arraylist?

[英]How to create an arraylist from input of a single jtextfield?

I want the user to be able to type in 10 different names in the same jTextField that I have implemented. 我希望用户能够在已实现的同一jTextField中键入10个不同的名称。 I want all the names to be stored in an array, which I can then access in a document. 我希望所有名称都存储在数组中,然后可以在文档中访问它。 Question: How do I implement the arraylist? 问:如何实现arraylist? I got this code from a wesbite. 我从韦斯比特那里得到了这段代码。 How do I incorporate the for loop in there to ask the user for a name 10 times? 如何在其中合并for循环以要求用户输入10次名称?

 ArrayList<String> nameArrayList = new ArrayList<String>();
 JTextField nameTextField = new JTextField();
 nameArrayList.add(nameTextField.getText());

Try this: 尝试这个:

nameArrayList.addAll(Arrays.asList(nameTextField.getText().split(","))); nameArrayList.addAll(Arrays.asList(nameTextField.getText()。split(“,”))));

If you use other separator in the input please change it accordingly! 如果在输入中使用其他分隔符,请相应更改!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM