简体   繁体   English

如何通过单击按钮将单独的字符串添加到数组并通过单击不同的按钮从数组中删除字符串?

[英]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.

相关问题 如何在单击按钮时显示不同的字符串 - How to display different strings on button click 如何在Android中单击按钮时向数组添加值 - How to add values to an array on button click in android 如何在java中将不同数组的字符串添加到一个数组中 - how to add strings from different arrays into one array in java 如何在Android中单击按钮后从GridView中删除按钮? - How to delete button from GridView after click the button in Android? 如何将字符串数组从单独的方法添加到 JList model? - How can I add an array of strings from a separate method to a JList model? 试图从带有字符串和整数的数组中分离字符串(包括当前程序) - trying to separate strings from an array with strings and ints (current program included) JavaFX:从数组显示的文本字段,数组更新/按钮单击增加 - 如何强制场景重绘更新的数组 - JavaFX: TextFields displayed from array, array updated / increased on button click - how to force the scene to redraw the updated array 当项目在strings.xml中列出而不是在Android Studio中作为静态时,如何删除在Spinner中选择的项目按钮? - How to delete the item selected in a Spinner on button click when the items are listed in strings.xml and not as static in Android Studio? android:如何在单独的按钮上单击按钮时在android中播放声音 - android: how to play sound in android on button click from a separate class 如何从Android按钮单击运行单独的应用程序 - How to run separate application from Android button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM