initialize string array

G

Garg

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
 
E

Eric Sosman

Garg said:
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." };
 
N

Nigel Wade

Garg said:
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"};
 
L

Lew

Nigel said:
Garg said:
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/a...ml#fill(java.lang.Object[], java.lang.Object)>
 
L

Lord Zoltar

Garg said:
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

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! ;)
 
G

Garg

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
 
R

RedGrittyBrick

Garg said:
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 = "";

or maybe (untested)
String[] foo = "Tarug|Garg||||||||".split("\\|");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top