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?
suppose I am getting 2 strings
and I want to store them in
how to do this?
| |||
feedback
|
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.
You are reading strings dynamically, and you want to end up with an array of them ( 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
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):
and then you can create and fill the array you really wanted. There is a convenience method on
It is not obvious at first glance why the | ||||
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:
Otherwise (if it's declared elsewhere) you can use:
If this isn't what you're trying to achieve, please clarify your question. | |||||||||||
feedback
|
Well, you can do it directly just by doing
If you mean you get the Strings at run time you can just do:
| |||||
feedback
|
String
? – COD3BOY Oct 11 '11 at 11:40