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.

Sorin D.
  • 91
  • 3
  • 12
  • Possible duplicate: https://stackoverflow.com/questions/17837117/java-sending-multiple-parameters-to-method – SedJ601 Dec 15 '17 at 14:15
  • Possible duplicate of [Java: Sending Multiple Parameters to Method](https://stackoverflow.com/questions/17837117/java-sending-multiple-parameters-to-method) – SedJ601 Dec 15 '17 at 14:15
  • 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. – Alex Savitsky Dec 15 '17 at 14:16
  • 8
    `myMethod(new myObject[]{obj1, obj2}, new String[]{"string1","string2"}, new Integer[]{123,456});`? – Lino Dec 15 '17 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. – Jack Dec 15 '17 at 14:17
  • @Jack yeah, actually I know what values I need to give the method, and I don't need to use them anywhere else so it makes no sense storing them. I'd like more flexibility when calling the method. Also, the parameter types are fixed. – Sorin D. Dec 15 '17 at 14:24
  • 1
    Why don't you input them directly in your code, if you know the values ? – Yassine Badache Dec 15 '17 at 14:26
  • 2
    Your comment suggest that this question may be yet another case of [X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Pshemo Dec 15 '17 at 14:28
  • @Pshemo what I meant is that I want an easier way to use this method. I will have to use it multiple times in different situations, and for each situation the individual values of the 3 parameters will be different. Both I and the others who will use the method would find it easier to just pass a simple, readable set of values. – Sorin D. Dec 15 '17 at 14:38
  • In that case you may want o use List instead of arrays. Since Java 9 we have List.of(....). So your code could look like `myMethod(List.of(a,b,c), List.of(e,f,g), List.of(1,2,3,4));`. In earlier versions we also have `Arrays.asList(...)`. – Pshemo Dec 15 '17 at 14:43
  • @Pshemo that is also a very good suggestion, I will try it when I have the chance. Unfortunately our project is based on Java 7 and there are plans on supporting Java 8, but I don't think we'll get approval for Java 9 for the sole purpose of me writing better methods. Thank you for your comment. – Sorin D. Dec 15 '17 at 15:05
  • For earlier versions you can use `Arrays.asList(...)` instead of `List.of`. Use static import for `asList` and you can write `myMethod(asList("foo","bar"), asList(1,2,3))`. – Pshemo Dec 15 '17 at 15:08

2 Answers2

5

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)) 
xs0
  • 2,990
  • 17
  • 25
  • Yes, actually this does look like a more elegant solution, yet it adds layer of abstraction. Thank you! – Sorin D. Dec 15 '17 at 14:42
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}
);
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Kirill
  • 449
  • 3
  • 11
  • 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()} – Jeremy Rako Dec 15 '17 at 14:30
  • 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. – Sorin D. Dec 15 '17 at 14:33
  • 1
    @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. – Pshemo Dec 15 '17 at 14:34
  • 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 ^ – Cowboy Farnz Dec 15 '17 at 14:38
  • @Pshemo This answer doesn't *declare* any arrays. I assume the original question wanted to avoid variable declarations. – markusk Dec 15 '17 at 14:38
  • @markusk "I assume the original question wanted to avoid variable declarations" yes, that *may* be what OP had in mind, but IMO it is not clear. – Pshemo Dec 15 '17 at 14:48
  • @markusk It's not a matter of avoiding declarations, or instantiating objects, but more like ease of writing and readability. – Sorin D. Dec 15 '17 at 15:10