任何人都可以帮我这个。让我说我有这个字符串数组,例如。
String fruit[]={"apple","orange","kiwi","apple","kiwi"}
我希望在每个单独的字符串变量中分配它。例如String box1="apple"
,String box2="oranges"
,String box3="kiwi"
。我怎样才能实际复制数组的值并将其存储在字符串变量中。任何人都可以知道如何做到这一点。
抱歉!我忘了将此添加到我的问题中,如果它在数组中检测到重复,它应该忽略重复值。例如串水果{“apple”,“kiwi”,“apple”}。输出Box1 =“apple”,Box2 =“kiwi”。
答案 0 :(得分:4)
String box1 = fruit[0];
String box2 = fruit[1];
等等。
答案 1 :(得分:2)
通过阅读评论,听起来你想要从字符串数组中获取唯一的字符串。可以使用Set
(HashSet
)轻松实现。
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.