Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds


  Asked By: James    Date: Nov 14    Category: Java    Views: 1738

Is the following statement permited: String s[]? if yes, what is its



6 Answers Found

Answer #1    Answered By: Landra Schmidt     Answered On: Nov 14

String s[];

will just declare a String array  named "s" without
array length nor any values.

u can c my example for any idea...

public class ArrayDemo
public static void main(String args[])
String s[]; // Declare Array
s = new String[3]; // Initialize array with the

s[0] = "Apple"; // Assign value for array's first
s[1] = "Mango"; // Assign value for array's second
s[2] = "Banana"; // Assign value for array's third

for(int i=0;i<s.length;i++)
System.out.println((i+1) + ". " + s[i]); ////
Prints the array

Answer #2    Answered By: Alexander Bouchard     Answered On: Nov 14

Yes, what you wrote is permitted, but you cannot use it like that, you need to
initialize before using that. So in order to initialize that you need to specify
its size. If you want an array  which is variable, use Vector class.

Answer #3    Answered By: Erika Evans     Answered On: Nov 14

As I droped java a long time, correct me if I'm wrong

I remember this statement  is only use as a "declare and initialize"
statement, such as:

String arr[]={"hello", "world"]; // two element array

The number of elements is counted during complie. But a single
String s[];

I didn't see that ever. Maybe a compile error pops to request the number
of array, or a default length, 0, is added to it. If the latter one is
true, since an array  object cannot be appended at runtime, it's just a
String[] all the time, maybe it can be used with a RTTI purpose, I'm not
sure it can pass compiler. Have a try, I cannot try since no JDK
installed in the computer I'm just writting.

Answer #4    Answered By: Jermaine Powell     Answered On: Nov 14

i'm new to java and i m trying do a simple program using netbeans.
but the line jTextField2.setText(vArray[0]); dont execute.

what i can do to run?


private void click(java.awt.event.MouseEvent evt)
// TODO add your handling code here:


public String funcaoDivideString(String vParametro)

String[] vArray = null;

vArray = vParametro.split(".");


return vParametro;

Answer #5    Answered By: Frank Butler     Answered On: Nov 14

Well, the problem is that you are passing an array  into a method
that only accepts a string  object. Another way to get desired
effect when you have an array of Strings and you want to pass it all
into a function that requires a String would be to loop through the
entire array. For example:

String finalText = "";

for (int i = 0; i < vArray.length; i++)
finalText += vArray[i] + " ";

finalText = finalText.trim();

This will create a String that contains all of the text from the
array and place it into the text field. If this was what you were
wanting to do, then I would suggest to do something like the

public String funcaoDivideString(String vParametro)

String[] vArray = vParametro.split(".");


return vParametro;

Also, I noticed that you are returning the same string that you are
passing in. Just to let you know, Java uses "pass-by-value" so the
String passed in cannot be modified and retain it's value once the
method is completed. Perahps you wanted to set "jTextField2" to ONE
of the values in the array? If this was the case then you would do
something like this:

jTextField2.setText(vArray[0]); //This will push in the first value.


jTextField2.setText(vArray[1]); //This will push in the second value.


jTextField2.setText(vArray[2]); //This will push in the third value.

I am not sure what exactly you wanted your function to do so I can't
give you any other example. I am guessing from the name of it, you
wanted to split the array for some reason?

Answer #6    Answered By: Francis Riley     Answered On: Nov 14

The problem is that the split function uses regexp and not a simple

The dot has a specific signification: a joker for all chars.
To split your string  in a array  of string, you have to use:

With your exemple, you will obtain a String[] with three elements.

Didn't find what you were looking for? Find more on Array Or get search suggestion and latest updates.