Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help with passing arrays as arguments

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with passing arrays as arguments

    Hello, I have come across a small dilemma. I am having trouble passing an array as an argument without first assigning it to a variable. I have a constructor that takes one of argument of a String array. I am successfully able to get it to work properly by doing this:

    public static String lev1a[] = {
    "1111111111111111111111",
    "1000000000100000000001",
    "1000710000020202020201",
    "1000011111002020202021",
    "1000000000000000000001",
    "1111111111111111111111",
    };
    Level level = new Level(lev1a);

    however, I can't find a way to pass a string array as an argument in a way that I don't need to name it/ assign first. I wanted to be able to do something along the lines of this:

    Level level = new Level(
    "1111111111111111111111",
    "1000000000100000000001",
    "1000710000020202020201",
    "1000011111002020202021",
    "1000000000000000000001",
    "1111111111111111111111",);

    but I can't get this to work. Is there a way to do this/ what is the proper syntax for it?


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help with passing arrays as arguments

    Quote Originally Posted by Jumbosize View Post
    ...
    however, I can't find a way to pass a string array as an argument in a way that I don't need to name it/ assign first. ...
    //
    // Z.java
    //
    // How to initialize an anonymous array as an argument to a method.
    //
    //  Zaphod_b
    //
    public class Z {
     
        private String [] strArray;
     
        public static void main(String [] args) {
     
            Z z = new Z(new String[]{"123","456"}); // The argument is an anonymous String array.
     
            System.out.printf("strArray.length = %d\n", z.getStrArray().length);
     
            for (int i = 0; i < z.getStrArray().length; i++) {
                System.out.printf("strArray[%d]: %s\n", i, z.getStrArray()[i]);
            }
     
        } // End main
     
        public Z(String [] sA) {
            strArray = sA;
        }
     
        public String [] getStrArray()
        {
            return strArray;
        }
     
    } // End class definition

    Output:

    strArray.length = 2
    strArray[0]: 123
    strArray[1]: 456




    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with passing arrays as arguments

    Thank you! I don't think I would have guessed that on my own, I appreciate it.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Help with passing arrays as arguments

    There's also a variant. (not necessarily better, just different):

    Z z = new Z("123", "456");
     
    //...
     
    public Z(String... sA) {
        strArray = sA;
    }

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help with passing arrays as arguments

    Quote Originally Posted by pbrockway2 View Post
    There's also a variant...
    Thanks for pointing that out. I like it!


    Cheers!

    Z

Similar Threads

  1. passing arguments
    By Samaras in forum Java Theory & Questions
    Replies: 19
    Last Post: August 2nd, 2012, 06:47 AM
  2. Method is not applicable for the arguments
    By jbodary in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 23rd, 2012, 08:05 AM
  3. Passing Arrays Between Methods
    By kigroy in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 10th, 2011, 11:10 PM
  4. Tiny problem with arguments.
    By Melawe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 15th, 2011, 03:26 PM
  5. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM