[英]I want to use elements of an array as variables but how?
char [] abc ={a,b,c};// The array // 1)
for(int i = 0; i < abc.length; i++) { //for loop // 2)
String abc[i] = "yukarı"; // 3)
}
*/ 1) I need variables for example: a, b, c. */ 1) 我需要变量,例如:a、b、c。 But if I put them in an array(I need to reach them with numbers) they are not variables as you can see.但是如果我把它们放在一个数组中(我需要用数字来到达它们),它们不是你所看到的变量。
2) I will assign the elements of abc array to strings automatically. 2)我将自动将 abc 数组的元素分配给字符串。 But java does not accept that String abc[i] = "yukarı";但是 java 不接受 String abc[i] = "yukarı"; abc[i] is not supported in here.此处不支持 abc[i]。 Java want to see String x = "yukarı"; Java 想看 String x = "yukarı";
3) I need these a, b, c variables in array because I need to reach them with numbers. 3)我需要数组中的这些 a、b、c 变量,因为我需要用数字来访问它们。 If ı could assign a number to a string value all my problems were solved.如果我可以为字符串值分配一个数字,我所有的问题都解决了。
Can you suggest another way or help?你能提出另一种方法或帮助吗? If Is there any fail for my explanation sorry about that.如果我的解释有任何失败,对不起。 Thanks for your helps.感谢您的帮助。
1). 1)。 In here you define the array as a char array.在这里,您将数组定义为字符数组。 (can consider as a collection of chars) So you can not add a string to that. (可以考虑作为字符的集合)所以你不能添加一个字符串。 So you can write as follows,所以你可以写成如下,
String [] abc ={"a","b","c"};
for(int i = 0; i < abc.length; i++) {
abc[i] = "yukarı"; // like String a = "yukarı";
It seams that you have less knowledge in java arrays.So refer this link for more details.看来您对 java 数组的了解较少。因此请参阅此链接以获取更多详细信息。
2). 2)。 And you can use reflection to do so.你可以使用反射来做到这一点。 please refer this link请参考这个链接
try the below试试下面的
String [] abc ={a,b,c};
//abc[0] = "yukarı"; //I want to use all elements in abc like variables.
for(int i = 0; i < abc.length; i++) {
abc[i] = "yukarı"; // like String a = "yukarı";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.