Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

initialize string array

Thread view: 
Garg - 10 Jan 2008 11:54 GMT
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
Eric Sosman - 10 Jan 2008 13:02 GMT
> Hi ,
>
[quoted text clipped - 3 lines]
>
> 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." };

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Nigel Wade - 10 Jan 2008 13:03 GMT
> Hi ,
>
[quoted text clipped - 3 lines]
>
> 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"};

Signature

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

Lew - 10 Jan 2008 14:13 GMT
>> Hi ,
>>
[quoted text clipped - 14 lines]
>
>  String[] arr = { "a", "", "something else"};

Also,
<http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#fill(java.lang.Objec
t[],%20java.lang.Object
)>

Signature

Lew

Lord Zoltar - 10 Jan 2008 14:32 GMT
> Hi ,
>
[quoted text clipped - 5 lines]
>
> Tarun

Perhaps 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 I
think there's an overload for Object[]. Give it a shot! ;)
Roedy Green - 10 Jan 2008 14:50 GMT
>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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Chase Preuninger - 11 Jan 2008 00:18 GMT
Loop through every value, and set it how you want it.
Garg - 11 Jan 2008 04:17 GMT
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 ''.

Garg
RedGrittyBrick - 11 Jan 2008 12:18 GMT
> thanks for the help.
>
[quoted text clipped - 8 lines]
> 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("\\|");


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.