复制数组中的字符串值

时间:2012-01-23 21:36:00

标签: java

任何人都可以帮我这个。让我说我有这个字符串数组,例如。

String fruit[]={"apple","orange","kiwi","apple","kiwi"}

我希望在每个单独的字符串变量中分配它。例如String box1="apple"String box2="oranges"String box3="kiwi"。我怎样才能实际复制数组的值并将其存储在字符串变量中。任何人都可以知道如何做到这一点。

抱歉!我忘了将此添加到我的问题中,如果它在数组中检测到重复,它应该忽略重复值。例如串水果{“apple”,“kiwi”,“apple”}。输出Box1 =“apple”,Box2 =“kiwi”。

3 个答案:

答案 0 :(得分:4)

String box1 = fruit[0];
String box2 = fruit[1];

等等。

答案 1 :(得分:2)

通过阅读评论,听起来你想要从字符串数组中获取唯一的字符串。可以使用SetHashSet)轻松实现。

e.g。

String[] fruits = {"apple", "orange", "kiwi", "apple", "kiwi"};
Set<String> uniqueFruits = new HashSet<String>(Arrays.asList(fruits));
System.out.println(uniqueFruits);

打印:

[orange, kiwi, apple]

答案 2 :(得分:1)

您可以使用数组访问:

String box1 = fruit[0];
String box2 = fruit[1];
// etc.