从 ArrayList 的字符串中获取变量名称

Getting variable name from a string for ArrayList

我想知道是否可以从 Java 中的字符串中检索变量名,但这可以用在 ArrayList 中。我已经阅读了 Whosebug 上无数关于这样做的帖子,例如使用地图,但我不断收到 "The constructor ArrayList(String) is undefined." 我想要做的就是这样:

 int first = 1; //These can change between each other, depending on the circumstances, 
                  like second could equal 3.
 int second = 2;
 int third = 3;
 int fourth = 4;

            List<String> player1List = new LinkedList<String>(Arrays.asList(player1));
            List<String> player2List = new LinkedList<String>(Arrays.asList(player2));
            List<String> player3List = new LinkedList<String>(Arrays.asList(player3));
            List<String> player4List = new LinkedList<String>(Arrays.asList(player4));

                String FirstString = "player" + first + "List";
                String SecondString = "player" + second + "List";
                String ThirdString = "player" + third + "List";
                String FourthString = "player" + fourth + "List";

                List<String> fusedPlayer1 = new ArrayList<String>(FirstString);
                fusedPlayer1.addAll(FourthString);

                List<String> fusedPlayer1 = new ArrayList<String>(SecondString);
                fusedPlayer1.addAll(ThirdString);

player1player2player3player4都是字符串。现在,您可能想知道我为什么要这样做,但这只是一个例子,在我的实际程序中,有更好的理由使用这种方法。我是 Java 的初学者,所以请原谅我的知识不足... 谢谢!

更新

新代码:

int first = 1; //These can change between each other, depending on the circumstances, 
                      like second could equal 3.
     int second = 2;
     int third = 3;
     int fourth = 4;

                List<String> player1List = new LinkedList<String>(Arrays.asList(player1));
                List<String> player2List = new LinkedList<String>(Arrays.asList(player2));
                List<String> player3List = new LinkedList<String>(Arrays.asList(player3));
                List<String> player4List = new LinkedList<String>(Arrays.asList(player4));

                    String FirstString = "player" + first + "List";
                    String SecondString = "player" + second + "List";
                    String ThirdString = "player" + third + "List";
                    String FourthString = "player" + fourth + "List";

             Map<String, String> map = new HashMap<String, String>();
             map.put(FirstString,  "player" + first   + "List");
             map.put(SecondString, "player" + second  + "List");
             map.put(ThirdString,  "player" + third   + "List");
             map.put(FourthString, "player" + fourth  + "List");

        List<String> fusedPlayer1 = new ArrayList<String>();
        fusedPlayer1.add(map.get(FirstString));
        fusedPlayer1.add(map.get(FourthString));

        List<String> fusedPlayer2 = new ArrayList<String>();
        fusedPlayer2.add(map.get(SecondString));
        fusedPlayer2.add(map.get(ThirdString));

这一行:

List<String> fusedPlayer1 = new ArrayList<String>(FirstString);

告诉 Java 你想调用 ArrayList 构造函数,它以 String 作为参数。问题是 ArrayList 没有这样的构造函数,因此 "The constructor ArrayList(String) is undefined.".

ArrayListclass提供了三个构造函数,它们的签名如下:

public ArrayList(int initialCapacity) { ... }

public ArrayList() { ... }

public ArrayList(Collection<? extends E> c) { ... }

我怀疑您正在尝试做的是创建 ArrayList 并将参数 String 作为初始元素。由于它是单个 String 而不是一组 String,因此最好的做法是执行以下操作:

List<String> fusedPlayer1 = new ArrayList<String>();
fusedPlayer1.add(FirstString);

还需要注意的是,从Java7开始,你不再需要两次指定类型,所以你可以这样写:

List<String> fusedPlayer1 = new ArrayList<>();

编译器然后使用类型推断 计算出ArrayList 的泛型类型应该是什么。

根据您的一些评论,Map 构造似乎更适合您的需求:

Map<Integer, LinkedList<String>> players = new HashMap<>();

players.put(1, player1List);
players.put(2, player2List);
players.put(3, player3List);
players.put(4, player4List);

然后当您需要访问时,例如 player3List,您只需执行:

players.get(3); // This will return the LinkedList associated with the Integer 3.

最后,从上面的语法着色可以看出,Java 的约定是您的变量名应该是:

  • namedInCamelCase,即第一个单词全部小写,后面单词的首字母全部大写,或者
  • NAMED_IN_CAPS_WITH_UNDERSCORES 如果变量同时是 finalstatic.

Class 名字应该在 CamelCaseWithAnInitialCapital.

您要么需要将列表存储在地图中,要么使用二维数组,其中第一个维度是索引。使用反射也可以使用变量名(至少如果你的变量是字段),但这是非常糟糕的做法。

这是生成 4 个列表并使用整数变量寻址它们的方式:

int first = 0;
List<String>[] lists = new List<String>[4];
lists[0] = new ArrayList<String>();
lists[1] = new ArrayList<String>(); ...
List<String> firstList = lists[first];