469,281 Members | 2,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

How to create an array?

Well, not exactly, bu tclose to that.
I whish to call a method that has, say, String[] as
an argument. Now, i haven't been in touch with Java
in a while so i don't remember the syntax. When i go:

object.method (new String ["a", "b"]);

the compiler naggs about missing "]". How do i create an
array without explicitly assigning a variable name to it?

--

Všnligen
Konrad
---------------------------------------------------
phone #1: (+46/0) 708 - 70 73 92
phone #2: (+46/0) 704 - 79 96 95
url: http://konrads.webbsida.com
e-mail: ch************@bigfoot.com
-----------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------

Jul 17 '05 #1
3 30335
Konrad Den Ende <ch************@bigfoot.com> wrote:
Well, not exactly, bu tclose to that.
I whish to call a method that has, say, String[] as
an argument. Now, i haven't been in touch with Java
in a while so i don't remember the syntax. When i go:

object.method (new String ["a", "b"]);

the compiler naggs about missing "]". How do i create an
array without explicitly assigning a variable name to it?


give this a try ...

String[] myArray = new String[] { "foo", "bar" };

Sample program :

class qwer {
public static void main( String[] args ) {
String[] myArray = new String[] { "foo", "bar" };
for ( int i = 0; i < myArray.length; i++ )
System.out.println( myArray[ i ] );
}
}

--
If you live in a country run by committee, be on the committee.
-- Graham Summer
Jul 17 '05 #2
SJ
Konrad Den Ende wrote:
Well, not exactly, bu tclose to that.
I whish to call a method that has, say, String[] as
an argument. Now, i haven't been in touch with Java
in a while so i don't remember the syntax. When i go:

object.method (new String ["a", "b"]);

the compiler naggs about missing "]". How do i create an
array without explicitly assigning a variable name to it?


object.method (new String[] {"a", "b"});
Jul 17 '05 #3
Hello Konrad,

try this:
public class Test
{
private static String foo( String[] test )
{
StringBuffer sb = new StringBuffer();

for( int i = 0; i < test.length; i++ )
{
sb.append( test[ i ] + ", " );
}

return sb.toString();
}

public static void main(String[] args)
{
System.out.println( foo( new String[] { "test1", "test2" } ) );
}
}
Best Regards
Silke
"Konrad Den Ende" <ch************@bigfoot.com> wrote in message news:<be**********@news.gu.se>...
String[] myArray = new String[] { "foo", "bar" };
System.out.println( myArray[ i ] );


How is that "create an array without explicitly assigning a
variable name to it"?

Všnligen
Konrad
---------------------------------------------------
phone #1: (+46/0) 708 - 70 73 92
phone #2: (+46/0) 704 - 79 96 95
url: http://konrads.webbsida.com
e-mail: ch************@bigfoot.com
-----------------------------------

Sleep - thing used by ineffective people
as a substitute for coffee

Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.