1

can anyone help me on this.Lets say I have this array of string e.g.

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

and I want to allocate it in each individual string variable. e.g. String box1="apple", String box2="oranges", String box3="kiwi". How can I actually copy the value of array and store it in string variable. Can anyone give idea on how to do this.

Sorry! I forgot to add this to my question if it detect a duplicate in array it should ignore the duplicate value . e.g. String fruit{"apple","kiwi","apple"}. Output Box1="apple", Box2="kiwi".

asked Jan 23, 2012 at 21:35
5
  • Can you explain why you would want to? The array removes the necessity of declaring/initializing multiple String variables. Commented Jan 23, 2012 at 21:38
  • Sorry! I forgot to add this to my question if it detect a duplicate in array it should ignore the duplicate value . e.g. String fruit{"apple","kiwi","apple"}. Output Box1="apple", Box2="kiwi". Commented Jan 23, 2012 at 21:46
  • @user1110191 Are you trying to store each index of the array into a variable or display each unique index of the array? Commented Jan 23, 2012 at 21:50
  • If that's the case, then you would want to look at this SO question and answer. I maintain that using an array to collect unique elements would be far easier, since you (normally) can't guarantee the number of elements you'll get, and it'll be far easier to code. Commented Jan 23, 2012 at 21:52
  • What I really want to do is read trough array and copy the value inside the array and each unique value in array will be stored in different variable. Similar to the example above. If it duplicate is detected then it will ignore the second one. Commented Jan 23, 2012 at 21:55

3 Answers 3

4

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

And so on.

answered Jan 23, 2012 at 21:37
Sign up to request clarification or add additional context in comments.

Comments

2

Reading through the comments, it sounds like you want to get the unique strings from the array of strings. It can be easily achieved using a Set (HashSet).

e.g.

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

That prints:

[orange, kiwi, apple]
answered Jan 23, 2012 at 22:03

Comments

1

You can just use an array access:

String box1 = fruit[0];
String box2 = fruit[1];
// etc.
Alex Lockwood
83.3k39 gold badges209 silver badges250 bronze badges
answered Jan 23, 2012 at 21:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.