Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I expand arguments in Java?

The following Python snippet does exactly what I mean:

def function(a, b, c):
    print("%i :: %s :: %f" % (a,b,c))

a = 1
b = "abc"
c = 1.0

function(a, b, c)

list = [a, b, c]

# This is what I am searching for in Java
function(*(list))

So I have one list-like structure and I don't know how many arguments it has, but I know that it has the right number of arguments with the right type and format. And I want to pass them to a method. So I have to "expand" those arguments. Does anybody know how to do so in Java?

like image 658
Martin Thoma Avatar asked Feb 16 '12 10:02

Martin Thoma


People also ask

Is it possible to expand a shape in Java?

It sounds like a theoretical example -- Java's Shape type doesn't have any expand method. Your teacher is telling you that you should implement this method on your own (presumeably in some Shape class that you will implement). Don't expect to find it anywhere... Especially in java.lang.Object.

How do you pass multiple arguments to a method in Java?

One using overloaded method (one for each) and another put the arguments into an array, and then pass this array to the method. Both of them are potentially error-prone and require more code. The varargs feature offers a simpler, better option.

Can We extend final method in Java?

- GeeksforGeeks Can We Extend final Method in Java? Final method in java can be extended but alongside the main concept to be taken into consideration is that extend means that you can extend that particular class or not which is having final method, but you can not override that final method.

Is there an expand( method in the object class?

Such a method would be applicable to a very small percentage of the classes that already exist (as hexafraction has pointed out). No, there is no expand ( method in the Object class, as it semantically isn't useful for (almost) all objects. Imagine trying to expand ( a socket or write binary data to a hexagon, for example.


3 Answers

Java doesn't have this facility, since as a statically, strongly-typed language it's uncommon for a method to take a collection of values of uniform type that could all be stored in some composite object (array, list, etc.). Python doesn't have this problem because everything is dynamically-typed. You can do this in Java by defining a helper method that takes a single object in holding all the parameters, then expands them out in a call to the real method.

Hope this helps!

like image 96
templatetypedef Avatar answered Oct 19 '22 09:10

templatetypedef


You could declare optional parameters with Object... such that you can pass an arbitrary number of arguments and then cast each of them to the required type:

public void method1(String p1, Integer p2) {
}

public void method2(Object... params) {
    method1((String)params[0], (Integer)params[1];
}

You can call method2 as:

method2("abc", 1);

Of course, this example is a bit redundant, because you could directly call method1, but that's not what I wanted to illustrate. :)

like image 42
Tudor Avatar answered Oct 19 '22 07:10

Tudor


You can't. In Java, method parameters are checked at compile time, and since the content of the array is only defined at runtime, this check would be impossible.

Your closest solution is to have a method taking an array as a parameter, and eventually expanding the arguments to place a call to the targeted method.

like image 32
Vivien Barousse Avatar answered Oct 19 '22 08:10

Vivien Barousse