我想知道是否可以从Java中的字符串中检索变量名,但是可以在ArrayList中使用它。我已经阅读了StackOverflow关于这样做的无数帖子,例如,使用地图,但我一直得到"构造函数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);
player1
,player2
,player3
和player4
都是字符串。现在,你可能想知道我为什么要这样做,但这仅仅是一个例子,在我的实际程序中,有一个更好的理由使用这个方法。我是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));
答案 0 :(得分:0)
这一行:
List<String> fusedPlayer1 = new ArrayList<String>(FirstString);
告诉Java您要调用以ArrayList
作为参数的String
构造函数。问题是ArrayList
没有这样的构造函数,因此“构造函数ArrayList(String)未定义。”。
ArrayList
类提供了三个构造函数,它们具有以下签名:
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);
还应该注意,从Java 7开始,您不再需要两次指定类型,因此您可以编写以下内容:
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
如果变量同时为final
和static
。班级名称应位于CamelCaseWithAnInitialCapital
。
答案 1 :(得分:0)
您需要将列表存储在地图中,或者使用两个dimmensional数组,其中第一个维度是索引。使用变量名也可以通过反射(至少如果你的变量是字段),但这是非常糟糕的实践。
这是生成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];