0

how can i store multiple strings in single array element in java? The strings may be added or deleted dynamically. So, the array has to be flexible. I mean, if there is an array a[], then:

 a[1] should be able to accommodate "String1", "String2", ....  
 a[2] should be able to accommodate "String3", "String4", ....   

These strings may be deleted at any time and additional strings can be added any time. What java component supports this kind of functionality? I am new to java and weak at basics. Hope u can help. Thanks !

4
  • 2
    can you use a ArrayList or array of ArrayList ? Commented Mar 1, 2013 at 16:44
  • that's what i would do, an arraylist of arraylists Commented Mar 1, 2013 at 16:45
  • 1
    I would go for Map<String, List<String>> Commented Mar 1, 2013 at 16:45
  • For real answer, more context is needed. With that info, a container containing containers containing strings is what you need, but which container classes you should use, impossible to say. Commented Mar 1, 2013 at 16:49

4 Answers 4

3

You probably need something like List<List<String>> yourList ;

Remove "String2" from your List:

yourList.get(0).remove("String2");

Add "StringX" to your List at index 0.

 yourList.get(0).add("Stringx");
Sign up to request clarification or add additional context in comments.

1 Comment

how do you access a particular string like "String1" in yourList(0) and how do you iterate thru all the strings in yourList(0) ? yourList(0) denotes all stings in array element 0
2

My suggestion is to try and learn Java collections!

Official Tutorials

Tutorial 1 Tutorial 2

This way you will have a collection for any situation you find yourself in

Comments

0

You can add this strings in the same string using some using a character as delimiter and after that use a split method.

Comments

0

Use Jagged arrays. I can get a program to you to know the basics of jagged arrays: Jagged Arrays – Varying Column Size Arrays

Comments

Your Answer

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.