简体   繁体   English

我想将数组的元素用作变量,但如何使用?

[英]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.

相关问题 如何通过输入向数组列表添加任意数量的元素? - How to add as many elements as i want to an array list by an input? 我如何在这里使用循环,我想每回合从列表中删除 3 个元素 - How can I use loop here, I want to remove 3 elements from list for every turn 如何使用变量指示数组长度? - How can I use variables to dictate array length? 我想将数组的所有元素包含到另一个数组中 - I want to contains all elements of array to another array 如果我想提取 JSON 响应值并避免在开头使用 static 变量,如何在 RestAssured 中使用 HashMaps? - How to use HashMaps in RestAssured if I want to extract the JSON response value and avoid using static variables at the beginning? 如何初始化数组1到10的元素,然后在将打印该元素的方法中使用该构造函数? - How do I initialize elements of an array, 1 through 10, and then use that constructor in a method that will print the elements? 如果我想设置数组的大小并且我想在空白点中使用空值,我将使用什么类型的数组 - What type of array do I use if I want to set the size of the array and I want nulls in the empty spots 如何检查字符串数组的长度? 我不想使用 lenght() 因为我需要知道数组中有多少个字符串 - How to check the lenght of an array of strings? I don't want to use lenght() because I need to know how many strings are in the array 如何使用Spring Boot在Post方法中传递Json数据? 我想传递几个变量,并在另一个java类中使用该变量 - how can i pass Json data in a Post method using spring boot? I want to pass few variables and use that variables in a different java class 如何在 java 中的映射或关联数组中使用变量 - how to use variables in maps or associative array in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM