How to create an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Konrad Den Ende

    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: chamsterkonrad@ 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
    ---------------------------------------------------





  • Pedro Sam

    #2
    Re: How to create an array?

    Konrad Den Ende <chamsterkonrad @bigfoot.com> wrote:[color=blue]
    > 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?[/color]

    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.prin tln( myArray[ i ] );
    }
    }



    --
    If you live in a country run by committee, be on the committee.
    -- Graham Summer

    Comment

    • SJ

      #3
      Re: How to create an array?

      Konrad Den Ende wrote:
      [color=blue]
      > 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?
      >
      >[/color]

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

      Comment

      • Silke Stuehler

        #4
        Re: How to create an array?

        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.prin tln( foo( new String[] { "test1", "test2" } ) );
        }
        }


        Best Regards
        Silke


        "Konrad Den Ende" <chamsterkonrad @bigfoot.com> wrote in message news:<bel2ob$57 l$1@news.gu.se> ...[color=blue][color=green]
        > > String[] myArray = new String[] { "foo", "bar" };
        > > System.out.prin tln( myArray[ i ] );[/color]
        >
        > 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: chamsterkonrad@ 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
        > ---------------------------------------------------[/color]

        Comment

        Working...