initialize string array

  • Follow


Hi ,String[] arr = new String[3];this allocate the size but all cell will contain null.how can i initialize the string will 'a' or blank.Tarun
0
Reply Garg 1/10/2008 11:54:21 AM

Garg wrote:> Hi ,> > String[] arr = new String[3];> > this allocate the size but all cell will contain null.> > how can i initialize the string will 'a' or blank.     arr[0] = "a";     arr[1] = " ";     arr[2] = "I hope this helps."Alternatively,     String[] arr = { "a", " ", "I hope this helps." };-- Eric Sosmanesosman@ieee-dot-org.invalid
0
Reply Eric 1/10/2008 1:02:54 PM


Garg wrote:> Hi ,> > String[] arr = new String[3];> > this allocate the size but all cell will contain null.> > how can i initialize the string will 'a' or blank.> The Java Tutorial is a good place to look for answers. This section coversarrays, and initialization:http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.htmlReading the entire tutorial would help you with the basics, and give you a muchbetter understanding of the language.One answer to your question might be: String[] arr = { "a", "", "something else"};-- Nigel Wade, System Administrator, Space Plasma Physics Group,            University of Leicester, Leicester, LE1 7RH, UK E-mail :    nmw@ion.le.ac.uk Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555
0
Reply Nigel 1/10/2008 1:03:23 PM

Nigel Wade wrote:> Garg wrote:> >> Hi ,>>>> String[] arr = new String[3];>>>> this allocate the size but all cell will contain null.>>>> how can i initialize the string will 'a' or blank.>>> > The Java Tutorial is a good place to look for answers. This section covers> arrays, and initialization:> http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html> > Reading the entire tutorial would help you with the basics, and give you a much> better understanding of the language.> > > One answer to your question might be:> >  String[] arr = { "a", "", "something else"};Also,<http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#fill(java.lang.Object[],%20java.lang.Object)>-- Lew
0
Reply Lew 1/10/2008 2:13:25 PM

Garg wrote:> Hi ,>> String[] arr = new String[3];>> this allocate the size but all cell will contain null.>> how can i initialize the string will 'a' or blank.>> TarunPerhaps you want:String[] arr = new String[3];Arrays.fill(arr,'a');I know this works on char[], I've never tried it with String[] but Ithink there's an overload for Object[]. Give it a shot! ;)
0
Reply Lord 1/10/2008 2:32:33 PM

On Thu, 10 Jan 2008 03:54:21 -0800 (PST), Garg <sendtogarg@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>String[] arr = new String[3];
>
>this allocate the size but all cell will contain null.
>
>how can i initialize the string will 'a' or blank.

see http://mindprod.com/jgloss/array.html
-- 
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
0
Reply Roedy 1/10/2008 2:50:28 PM

Loop through every value, and set it how you want it.
0
Reply Chase 1/11/2008 12:18:26 AM

thanks for the help.what i am doing isString[] arr;String str = "Tarun|garg";arr = str.split("\\|");now this will split the str and arr will contain two values.what i need to do is i have to fix the array like 10 and rest 8 fieldsof the array should contain ''.Garg
0
Reply Garg 1/11/2008 4:17:03 AM

Garg wrote:> thanks for the help.> > what i am doing is> > String[] arr;> String str = "Tarun|garg";> > arr = str.split("\\|");> > now this will split the str and arr will contain two values.> what i need to do is i have to fix the array like 10 and rest 8 fields> of the array should contain ''.> Eric already answered this, didn't you read his reply?   String[] foo = { "Tarug", "Garg", "", "", "", "", "", "", "", "" };or (easier to apply to larger arrays)   String[] foo = new String[10];   foo[0] = "Tarug";   foo[1] = "Garg";   for (int i=2; i< foo.length; i++) foo[i] = "";or maybe (untested)   String[] foo = "Tarug|Garg||||||||".split("\\|");
0
Reply RedGrittyBrick 1/11/2008 12:18:05 PM

8 Replies
185 Views

(page loaded in 0.124 seconds)

Similiar Articles:













7/7/2012 4:55:48 PM


Reply: