[英]How to add separate strings to array from button click and delete strings from array with a different button click?
I am wondering how to add strings to an array from a button click, this is what I have so far but only one text value is written to the array the rest are empty我想知道如何通过单击按钮将字符串添加到数组中,这是我目前所拥有的,但只有一个文本值写入数组,其余为空
private void addTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {
String teamArray []=new String[5];
teamArray[0]= addTeamText.getText();
addTeamText.setText("");
teamArray[1]= addTeamText.getText();
addTeamText.setText("");
teamArray[2]= addTeamText.getText();
addTeamText.setText("");
teamArray[3]= addTeamText.getText();
addTeamText.setText("");
teamArray[4]= addTeamText.getText();
What I want to do is add multiple teams to a list and then be able to delete them from the array with a button called say "deleteTeamButton" if anybody has any pointers on how to implement this i would be very grateful?我想要做的是将多个团队添加到列表中,然后能够使用名为“deleteTeamButton”的按钮将它们从数组中删除,如果有人有任何关于如何实现这一点的指示,我将不胜感激?
As you just said, you only need to initialize an array list and add items in your on click function.正如您刚才所说,您只需要初始化一个数组列表并在您的点击功能中添加项目。
ArrayList<String> Teams = new ArrayList<>();
private void addTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {
String Team = addTeamText.getText();
System.out.println (Team);
Teams.add(Team);
addTeamText.setText("");
} }
The same for the function that deletes items.删除项目的功能也是如此。 You can look for ArrayList documentation for the deletion process.您可以查找有关删除过程的 ArrayList 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.