1

I want to be able to write an array directly as a parameter for my method, instead of storing the array values inside a separate variable.

Example:

public void myMethod(myObject[] objParam, String[] strParam, Integer[] intParam) {
    // do stuff
}

And I would like to call the method like this:

myMethod({obj1, obj2}, {"string1","string2"}, {123,456});

Currently this is not acceptable in my IDE, and I've tried different notations, even casting to arrays, however nothing works.

I don't want to have to declare and initiate an array for each of the parameters every time I need to use my method.

Does anyone know a good workaround for this? Is using List a solution?

EDIT: The parameter types are fixed, the array values will correspond 1 to 1 for each of the 3 parameters.

12
  • 1
    new Object[]{obj1, obj2} should do it, or is that exactly what you're trying to avoid in the first place? I'm afraid there's no shorter way to specify an array in Java. And List/Set/Map is even more verbose. Commented Dec 15, 2017 at 14:16
  • 9
    myMethod(new myObject[]{obj1, obj2}, new String[]{"string1","string2"}, new Integer[]{123,456});? Commented Dec 15, 2017 at 14:16
  • 2
    "I don't want to have to declare and initiate an array for each of the parameters", well you want to pass 3 array arguments, I guess having to initialize them is part of the story. Commented Dec 15, 2017 at 14:17
  • 1
    Why don't you input them directly in your code, if you know the values ? Commented Dec 15, 2017 at 14:26
  • 2
    Your comment suggest that this question may be yet another case of X/Y problem. Commented Dec 15, 2017 at 14:28

2 Answers 2

4

The shortest way I can think of is preparing a class like this:

public class ArrUtil {
    public static Integer[] arr(Integer... elements) {
        return elements;
    }

    public static String[] arr(String... elements) {
        return elements;
    }

    public static myObject[] arr(myObject... elements) {
        return elements;
    }
}

Then you can statically import arr:

import static ArrUtil.*;

And use it like this:

myMethod(arr(obj1, obj2), arr("abc", "Def"), arr(1, 2));

Edit: you say the values are correlated. A cleaner solution would be to declare a container class:

class Foo {
    final myObject obj;
    final String str;
    final int num;

    Foo(myObject obj, String str, int num) {
        this.obj = obj;
        this.str = str;
        this.num = num;
    }

    static Foo Foo(myObject obj, String str, int num) {
        return new Foo(obj, str, num);
    }
}

Then you can take a varargs parameter:

void myMethod(Foo... foos) {
}

And call it with

myMethod(Foo(obj1, "abc", 123), Foo(obj2, "def", 234)) 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, actually this does look like a more elegant solution, yet it adds layer of abstraction. Thank you!
4

To initialize an array inline, the type needs to be specified as well, like so:

myMethod(new myObject[]{obj1, obj2},
        new String[]{"string1", "string2"},
        new Integer[]{123, 456}
);

7 Comments

If "obj1" and "obj2" are two objects of type "myObjects" that have been declared before the function call, this approach works. If you want everything to be created in this function call, you have to use new myObject[]{new myObject(), new myObject()}
Thank you, this solves the issue and the code will at least compile, but I'll have to think of a more elegant way of writing it. Perhaps passing strings and parsing each of them inside the method.
@SorinD. "I don't want to have to declare and initiate an array for each of the parameters every time I need to use my method." and yet you accepted answer which does exactly what you wanted to avoid. new Type[]{elements, ...} initialize an array and you need to do this for each group of parameters.
While this will compile, I find xs0 below to have the most elegant and relevant approach to your problem given what we know i.e. what Pshemo has confirmed above ^
@Pshemo This answer doesn't declare any arrays. I assume the original question wanted to avoid variable declarations.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.