[英]Java: Add to array by JOptionPane
I have this array: 我有这个数组:
String[] countriesList = {
"Japan",
"Sverige",
"Tyskland",
"Spanien",
"Syrien",
"Litauen",
};
I want to be able to add another thing to the array, in this case this [6]th position. 我希望能够在该数组的第[6]个位置添加其他内容。 Is it possible to do this by JOPtionPane?
JOPtionPane是否可以做到这一点? This is what I've done this far, however nothing happens nor does any errors occur.
这就是我到目前为止所做的,但是什么也不会发生,也不会发生任何错误。
String addland = JOptionPane.showInputDialog("Vilket land vill du lägga till?").trim();
countriesList[6] = addland;
Arrays
start their counting from 0, so you could use countriesList[5] = addland;
Arrays
从0开始计数,因此您可以使用countriesList[5] = addland;
You may use a dynamic list
to perform your task. 您可以使用
dynamic list
执行任务。 They are better in every situation and should be superior to simple Arrays
它们在每种情况下都更好,并且应该优于简单
Arrays
Try to use this 尝试使用这个
List<String> countriesList = new ArrayList<>(
Arrays.asList("Japan", "Sverige", "Tyskland", "Spanien", "Syrien", "Litauen"));
String addland = JOptionPane.showInputDialog("Vilket land vill du lägga till?").trim();
countriesList.set(5,addland);
System.out.println(countriesList);
Output, after entering asdadsad
: 输入
asdadsad
后的输出:
[Japan, Sverige, Tyskland, Spanien, Syrien, asdadsad]
To add a land to the existing list use countriesList.add(addland);
要将土地添加到现有列表中,请使用
countriesList.add(addland);
instead of countriesList.set(5,addland);
代替
countriesList.set(5,addland);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.