简体   繁体   English

如何使用Java将数组参数设置为变量

[英]How do I set array arguments into variables using Java

I want to take this string "John Doe and Eli Manning", and using split() on the String to store the elements of the array into variables. 我想使用此字符串“ John Doe和Eli Manning”,并在String上使用split()将数组的元素存储到变量中。

I already know how to do the first part 我已经知道如何做第一部分

String str = "John Doe and Eli Manning"; 
String tokens[] = str.split("and");

Now I have an array ["Kevin Suffredini", "Eli Manning"]. 现在,我有了一个数组[“ Kevin Suffredini”,“ Eli Manning”]。 I want to set those two elements into separate variables that I can use. 我想将这两个元素设置为可以使用的单独变量。 What is the syntax for this? 这是什么语法?

something like?? 就像是?? :

String person1 = ...arg[0];
String person2 = ...arg[1];

Use the tokens array you defined, like 使用您定义的tokens数组,例如

String p1 = tokens[0];
String p2 = tokens[1];

You probably want to make sure your tokens array has 2 items in it. 您可能要确保tokens数组中有2个项目。

Also, you can't dynamically create variables if you can have an arbitrary amount tokens, but you can loop over tokens and get each String in the array. 另外,如果可以具有任意数量的标记,则不能动态创建变量,但是可以循环标记并获取数组中的每个String。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM