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

Java 从ArrayList的字符串中获取变量名,java,arraylist,linked-list,Java,Arraylist,Linked List,我想知道在Java中是否可以从字符串中检索变量名,但这可以在ArrayList中使用。我已经阅读了无数关于StackOverflow的帖子,例如使用maps,但我一直得到“构造函数ArrayList(String)未定义”的信息。我想做的就是: int first = 1; //These can change between each other, depending on the circumstances, like second could equ

我想知道在Java中是否可以从字符串中检索变量名,但这可以在ArrayList中使用。我已经阅读了无数关于StackOverflow的帖子,例如使用maps,但我一直得到“构造函数ArrayList(String)未定义”的信息。我想做的就是:

 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);
这一行:

List<String> fusedPlayer1 = new ArrayList<String>(FirstString);
我怀疑您试图做的是创建
ArrayList
,并将参数
String
作为初始元素。由于它是一个
字符串
而不是一组
字符串
s,因此最好执行以下操作:

List<String> fusedPlayer1 = new ArrayList<String>();
fusedPlayer1.add(FirstString);
List<String> fusedPlayer1 = new ArrayList<>();
然后,当您需要访问(例如,
player3List
)时,只需执行以下操作:

players.get(3); // This will return the LinkedList associated with the Integer 3.
最后,从上面的语法着色可以看出,Java的约定是变量名应为:

  • namedInCamelCase
    ,即第一个单词都是小写,后面单词的第一个字母都是大写,或者
  • 如果变量同时是
    final
    static
    ,则在\u CAPS\u中使用带下划线的\u命名

类名应位于具有初始资本的camelcase中。您需要将列表存储在地图中,或者使用二维数组,其中第一个维度是索引。在反射中也可以使用变量名(至少在变量是字段的情况下是这样),但这是非常糟糕的做法

这就是生成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];
int first=0;
列表[]列表=新列表[4];
列表[0]=新的ArrayList();
列表[1]=新的ArrayList()。。。
List firstList=列表[第一];

谢谢你指出这一点,我一定会修复它。不过,为了确保,这是在将
FirstString
链接到
player+first+List
,最后链接到
player1List
,对吗?@PhilipBecker您所做的只是创建一个
String
,其中包含文本
“playerXList”
然后将
字符串
对象存储在
数组列表
中-仅此而已。是否有办法让程序知道,例如,包含player1List的字符串正在引用名为player1List的列表?@PhilipBecker不容易。您最好将每个
LinkedList
存储在一个
映射中,由
字符串或
整数键入,然后您可以使用该键访问相应的
LinkedList
。@PhilipBecker否-您没有在
映射中放入任何内容以使其返回。
Map
也需要正确的类型,因此类似于
Map
而不是
Map
Map<Integer, LinkedList<String>> players = new HashMap<>();

players.put(1, player1List);
players.put(2, player2List);
players.put(3, player3List);
players.put(4, player4List);
players.get(3); // This will return the LinkedList associated with the Integer 3.
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];