如何从用户交互中生成新变量?

时间:2014-04-29 00:45:03

标签: java eclipse variables

我正在使用Eclipse并且有一个通用的编程问题。每当用户按下按钮时,我想创建一个新变量。如果它是一个变量,那么它是直接的;在按下按钮的代码中我会声明 -

String title1 =“Title1”;

但是,我希望每次用户点击按钮时都会生成一个新的不同变量。我尝试了不同的东西,比如for循环来修改下一个变量的名称,但是所有的东西都用完了想法,所以来这里问我的第一个问题。

谢谢大家,如果这是一个愚蠢的问题,请道歉。

4 个答案:

答案 0 :(得分:1)

你说的不能做。您可以执行的操作是使用ArrayList<String>向用户点击添加String

SSCCE:

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));
    }
}  

来源:http://examples.javacodegeeks.com/core-java/util/arraylist/arraylist-in-java-example-how-to-use-arraylist/

答案 1 :(得分:0)

使用ArrayList<String>LinkedList<String>。基于数组的集合可以通过数字索引访问无限数量的对象(将它们视为单独的变量)。

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<String,String>

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.

值可以是任何对象类型,而不仅仅是String

答案 2 :(得分:0)

使用Map而不是拥有单个变量,您可以根据需要添加任意数量的条目。

答案 3 :(得分:0)

为什么每次用户点击按钮时都要创建一个新变量。我的意思是浪费资源。此外,如果您想优化您的程序,请按照上述专家的建议使用Map或List。同意你们。