suppose I am getting 2 strings

"About product" and "About Us"

and I want to store them in String array like

myArray[]={"About product", "About Us"};

how to do this?

share|improve this question

56% accept rate
why dont you store them in String ? – COD3BOY Oct 11 '11 at 11:40
You can use Collection instead of array and use Collection.add(Object) method for dynamic needs – Alex K Oct 11 '11 at 12:01
feedback

closed as too localized by Harry Joy, trojanfoe, Lasse V. Karlsen Oct 12 '11 at 11:16

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

4 Answers

up vote 0 down vote accepted

You are reading strings dynamically, and you want to end up with an array of them (String[]), but you don't know how many there are until you've read them all.

In Java, arrays are of fixed length, so the answer is not to store them in an array until you know how many there are. What you need is a temporary list. There are several lists you can use, depending upon certain performance characteristics, but the ArrayList is a reasonable choice.

ArrayList<String> arrList = new ArrayList<String>();

will create a new array list. Then in a loop you can read a string and add it to the list (which will expand to accommodate each new entry):

arrList.add(str);

and then you can create and fill the array you really wanted. There is a convenience method on Lists for doing this, like this:

String[] strArray = arrList.toArray(new String[arrList.size()]);

It is not obvious at first glance why the toArray method needs to have the array passed to it, but it makes the call typesafe, and you don't have to cast the result to String[]. You can read the JavaDoc for this method here.

share|improve this answer
feedback

The code you've shown at the end is almost correct already. If you're declaring the variable at that point you can use:

String[] myArray = { firstString, secondString };

Otherwise (if it's declared elsewhere) you can use:

myArray = new String[] { firstString, secondString };

If this isn't what you're trying to achieve, please clarify your question.

share|improve this answer
i am getting strings dynamically. at first i do not know how many strings i will be getting. – Soniya Oct 11 '11 at 11:50
1  
Soniya: this is a new requirement, please amend the question. – Steve Powell Oct 11 '11 at 12:01
@Soniya: In that case you need to provide a lot more information. How are you getting the strings? At what point do you know how many you've got? Can you use a List<String> instead of an array? Please read tinyurl/so-hints for what makes a good question, and then edit your question appropriately. – Jon Skeet Oct 11 '11 at 12:19
I don't knew that this syntax of array declaration too works in C#. :) – The Mask Oct 12 '11 at 5:12
feedback

Well, you can do it directly just by doing

String[] myArray={"About product", "About Us"};

If you mean you get the Strings at run time you can just do:

String[] myArray = new String[2];

myArray[0] = s1;
myArray[1] = s2;
share|improve this answer
I am doing String myArray[]={}; and then adding Strings to myArray in a loop. it's not working. – Soniya Oct 11 '11 at 11:51
What you need is to allocate memory at run time because during compile time you don't know the data that you're going to store. This is done using the constructor (String myArray[] = new String[N]). String myArray[]={} won't work because you're allocating memory statically. You can only do that if you know what strings you'll save (String[] myArray={"About product", "About Us"} is OK because you are specifying the strings at that point). Hope this makes sense. – MsLovelace Nov 11 '11 at 16:36
feedback
String[] myArray = {"About product","About Us"};
share|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.