[英]Getting variable name from a string for ArrayList
I was wondering if it was possible to retrieve a variable name from a string in Java, but that can be used in an ArrayList.我想知道是否可以从 Java 中的字符串中检索变量名,但这可以在 ArrayList 中使用。 I have read the countless posts on StackOverflow about doing this, using maps, for example, but I keep getting "The constructor ArrayList(String) is undefined."
我已经阅读了 StackOverflow 上无数关于这样做的帖子,例如使用地图,但我一直收到“构造函数 ArrayList(String) 未定义”。 All I want to do is this:
我想做的就是这样:
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
and player4
are all strings. player1
、 player2
、 player3
和player4
都是字符串。 Now, you may be wondering why I want to do this, but this is just an example, and in my actual program, there is a better reason for using this method.现在,你可能想知道我为什么要这样做,但这只是一个例子,在我的实际程序中,使用这种方法有更好的理由。 I'm a very beginner at Java, so please forgive my lack of knowledge... Thanks!
我是 Java 的初学者,所以请原谅我缺乏知识......谢谢!
UPDATE更新
New code :新代码:
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));
This line:这一行:
List<String> fusedPlayer1 = new ArrayList<String>(FirstString);
Is telling Java that you want to invoke the ArrayList
constructor that takes a String
as an argument.告诉 Java 您要调用以
String
作为参数的ArrayList
构造函数。 The problem is that ArrayList
has no such constructor, hence "The constructor ArrayList(String) is undefined.".问题是
ArrayList
没有这样的构造函数,因此“构造函数 ArrayList(String) 是未定义的。”。
The ArrayList
class provides three constructors, and they have the following signatures: ArrayList
类提供了三个构造函数,它们具有以下签名:
public ArrayList(int initialCapacity) { ... }
public ArrayList() { ... }
public ArrayList(Collection<? extends E> c) { ... }
What I suspect you're trying to do is to create the ArrayList
with the argument String
as an initial element.我怀疑您试图做的是使用参数
String
作为初始元素创建ArrayList
。 As it's a single String
rather than a group of String
s, your best bet is to do the following:由于它是单个
String
而不是一组String
,因此最好的办法是执行以下操作:
List<String> fusedPlayer1 = new ArrayList<String>();
fusedPlayer1.add(FirstString);
It should also be noted that as of Java 7, you no longer need to specify the type twice, so you can write the following:还应该注意的是,从 Java 7 开始,您不再需要指定两次类型,因此您可以编写以下内容:
List<String> fusedPlayer1 = new ArrayList<>();
The compiler then uses type inference to work out what the generic type of the ArrayList
should be.然后编译器使用类型推断来确定
ArrayList
的泛型类型应该是什么。
Based on some of your comments, a Map
construct seems more suited to your needs:根据您的一些评论,
Map
构造似乎更适合您的需求:
Map<Integer, LinkedList<String>> players = new HashMap<>();
players.put(1, player1List);
players.put(2, player2List);
players.put(3, player3List);
players.put(4, player4List);
Then when you need to access, for example, player3List
, you simply do:然后,当您需要访问,例如
player3List
,您只需执行以下操作:
players.get(3); // This will return the LinkedList associated with the Integer 3.
Lastly, as you can see from the syntax colouring above, the conventions for Java are that your variable names should either be:最后,从上面的语法着色可以看出,Java 的约定是您的变量名应该是:
namedInCamelCase
, that is, the first word is all lower case and the first letter of subsequent words is upper case, or namedInCamelCase
,即第一个单词全部小写,后续单词的首字母大写,或者NAMED_IN_CAPS_WITH_UNDERSCORES
if the variable is both final
and static
. NAMED_IN_CAPS_WITH_UNDERSCORES
如果变量是final
和static
。 Class names should be in CamelCaseWithAnInitialCapital
.类名应该在
CamelCaseWithAnInitialCapital
。
You either need to store your lists in a map or you use a two dimmensional array where the first dimension is the index.您要么需要将列表存储在地图中,要么使用二维数组,其中第一个维度是索引。 Using variable names would also be possible with reflection (at least if your variables are fields), but this is extremly bad practize.
通过反射也可以使用变量名(至少如果您的变量是字段),但这是非常糟糕的做法。
This is how you would generate 4 lists and address them with an integer variable:这是生成 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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.