Java 如何从用户交互中生成新变量?
我正在使用Eclipse,有一个通用的编程问题。我想在用户每次按按钮之类的操作时创建一个新变量。如果一次生成一个变量,它将非常简单;在按下按钮的代码中,我要声明- 字符串title1=“title1” 但是,我希望在用户每次单击按钮时生成一个新的、不同的变量。我尝试了不同的方法,比如使用for循环来修改下一个变量的名称,但几乎没有任何想法,所以我来到这里问我的第一个问题 谢谢大家,如果这是一个愚蠢的问题,我道歉 使用ArrayList或LinkedList。基于数组的集合可以容纳无限数量的对象,可以将它们视为数字索引访问的独立变量Java 如何从用户交互中生成新变量?,java,eclipse,variables,Java,Eclipse,Variables,我正在使用Eclipse,有一个通用的编程问题。我想在用户每次按按钮之类的操作时创建一个新变量。如果一次生成一个变量,它将非常简单;在按下按钮的代码中,我要声明- 字符串title1=“title1” 但是,我希望在用户每次单击按钮时生成一个新的、不同的变量。我尝试了不同的方法,比如使用for循环来修改下一个变量的名称,但几乎没有任何想法,所以我来到这里问我的第一个问题 谢谢大家,如果这是一个愚蠢的问题,我道歉 使用ArrayList或LinkedList。基于数组的集合可以容纳无限数量的对象,
List<String> titles = new ArrayList<String>();
titles.add("Title1");
titles.add("Title2");
. . .
System.out.println(titles.get(1));
System.out.println(titles.get(2));
. . .
System.out.println("There are " + titles.size() + " titles:");
for (String title : titles) {
System.out.println(title);
}
如果希望通过符号名而不是索引来访问它们,请使用HashMap
值可以是任何对象类型,而不仅仅是字符串。使用映射而不是单个变量,并且您可以根据需要添加任意多的条目。您所说的无法完成。您可以改为使用ArrayList,在用户单击时可以向其中添加字符串 SSCCE:
来源:为什么每次用户单击按钮时都要创建新变量。我的意思是浪费资源。此外,如果您想使您的程序优化,然后使用上述专家建议的地图或列表。同意你们的观点。这不是一个愚蠢的问题,但却是一个常见的问题。首先考虑一下这个问题,因为对同一事物的更多讨论可能不会给这个网站带来更多的好处。例如,这里有一个:可能的复制品。投票结束这个问题作为重复。我不是一个经验丰富的程序员。我以为我到处寻找答案,但我想我是在寻找错误的关键词,比如生成新变量。如果你觉得这个问题应该删除,我不会反对。@user3583364如果复制品和它的目标使用不同的关键字,我们希望保留这两个关键字,因为其他人可能会使用相同的术语进行搜索。谢谢你的友好和迅速的答案。你确实帮助了我。
Map<String,String> titles = new HashMap<String,String>();
titles.put("title1", "The First Title");
titles.put("title2", "The Second Title");
. . .
System.out.println(titles.get("title1"));
// etc.
import java.util.*;
public class ArrayListExamples {
public static void main(String args[]) {
// Creating an empty array list
ArrayList<String> list = new ArrayList<String>();
// Adding items to arrayList
list.add("Item1");
list.add("Item2");
list.add(2, "Item3"); // it will add Item3 to the third position of
// array list
list.add("Item4");
// Display the contents of the array list
System.out.println("The arraylist contains the following elements: "
+ list);
// Checking index of an item
int pos = list.indexOf("Item2");
System.out.println("The index of Item2 is: " + pos);
// Checking if array list is empty
boolean check = list.isEmpty();
System.out.println("Checking if the arraylist is empty: " + check);
// Getting the size of the list
int size = list.size();
System.out.println("The size of the list is: " + size);
// Checking if an element is included to the list
boolean element = list.contains("Item5");
System.out
.println("Checking if the arraylist contains the object Item5: "
+ element);
// Getting the element in a specific position
String item = list.get(0);
System.out.println("The item is the index 0 is: " + item);
// Retrieve elements from the arraylist
// 1st way: loop using index and size list
System.out
.println("Retrieving items with loop using index and size list");
for (int i = 0; i < list.size(); i++) {
System.out.println("Index: " + i + " - Item: " + list.get(i));
}
// 2nd way:using foreach loop
System.out.println("Retrieving items using foreach loop");
for (String str : list) {
System.out.println("Item is: " + str);
}
// 3rd way:using iterator
// hasNext(): returns true if there are more elements
// next(): returns the next element
System.out.println("Retrieving items using iterator");
for (Iterator<String> it = list.iterator(); it.hasNext();) {
System.out.println("Item is: " + it.next());
}
// Replacing an element
list.set(1, "NewItem");
System.out.println("The arraylist after the replacement is: " + list);
// Removing items
// removing the item in index 0
list.remove(0);
// removing the first occurrence of item "Item3"
list.remove("Item3");
System.out.println("The final contents of the arraylist are: " + list);
// Converting ArrayList to Array
String[] simpleArray = list.toArray(new String[list.size()]);
System.out.println("The array created after the conversion of our arraylist is: "
+ Arrays.toString(simpleArray));
}
}