在Java中将参数从函数传递到内部函数?

在Java中将参数从函数传递到内部函数?,java,function,arguments,Java,Function,Arguments,编辑:为了澄清,我需要与python代码相当的java代码 如何将所有参数从一个函数传递给在其中调用的另一个函数。这在Python中可以通过以下方式实现: def p(*args): print(*args) 我知道在Java中可以使用对象传递多个参数。。。对象 但我不知道如何通过“解包”数组将它们传递给内部函数。我正在努力完成的一个例子: public static void printf(Objects object) { System.out.printf(objects

编辑:为了澄清,我需要与python代码相当的java代码

如何将所有参数从一个函数传递给在其中调用的另一个函数。这在Python中可以通过以下方式实现:

def p(*args):
    print(*args)
我知道在Java中可以使用
对象传递多个参数。。。对象
但我不知道如何通过“解包”数组将它们传递给内部函数。我正在努力完成的一个例子:

public static void printf(Objects object) {
    System.out.printf(objects)
    //I don't believe that this works, but it's the jist of what I want to do
}

有没有更好的方法来执行类似于此的操作或更类似于Java的方法?

您可以传递所有参数:

def func1(*args):
    print 'Hi, my name is func1'
    print 'Now I will call func2 with all args'
    func2(*args)
或者,如果您只想传递一些参数:

def func1(*args):
    print 'Hi, my name is func1'
    print 'Now I will call func2 with only two args'
    func2(args[1], args[5])

您可以传递所有参数:

def func1(*args):
    print 'Hi, my name is func1'
    print 'Now I will call func2 with all args'
    func2(*args)
或者,如果您只想传递一些参数:

def func1(*args):
    print 'Hi, my name is func1'
    print 'Now I will call func2 with only two args'
    func2(args[1], args[5])
你可以用

public static void foo(Object... args) {
for (Object arg : args) {
    System.out.println(arg); // do something here 
 }
}
从你想要的地方这样叫它

foo(“1”、“2”、“3”、1、1.21231,“你好”);//您可以传递任意数量的信息

您可以使用

public static void foo(Object... args) {
for (Object arg : args) {
    System.out.println(arg); // do something here 
 }
}
从你想要的地方这样叫它


foo(“1”、“2”、“3”、1、1.21231,“你好”);//对于
System.out.printf
的特定情况,您可以传递任意数量的数据。因为
printf
有一个参数列表
(字符串,对象…)
。您可以这样做:

private static void foo(String s, Object... objs) {
    System.out.printf(s, objs);
}
基本上,您可以将类型为
Object…
的参数传递给另一个需要
Object…
Object[]
的方法

但是,您不能将
对象…
传递给一个需要5个
对象的方法

private static void foo(Object... objs) {
    bar(objs)
}

private static void bar(Object a, Object b, Object c, Object d, Object e) {

}
您必须逐个传递参数:

bar(objs[0], objs[1], objs[2], objs[3], objs[4]);

对于
System.out.printf
的特定情况,这是非常可能的。因为
printf
有一个参数列表
(字符串,对象…)
。您可以这样做:

private static void foo(String s, Object... objs) {
    System.out.printf(s, objs);
}
基本上,您可以将类型为
Object…
的参数传递给另一个需要
Object…
Object[]
的方法

但是,您不能将
对象…
传递给一个需要5个
对象的方法

private static void foo(Object... objs) {
    bar(objs)
}

private static void bar(Object a, Object b, Object c, Object d, Object e) {

}
您必须逐个传递参数:

bar(objs[0], objs[1], objs[2], objs[3], objs[4]);

为什么您认为它不起作用?您可以创建对象并将所有变量存储在那里,但我认为在您的情况下,使用
*args
是可行的better@Mritunjay因为System.out.printf没有将数组作为参数
publicstaticvoidprintf(Object…o){System.out.printf(o);}
不起作用为什么您认为它不起作用?您可以创建对象并将所有变量存储在那里,但我认为在您的情况下使用
*args
是可行的better@Mritunjay因为System.out.printf没有将数组作为参数<代码>公共静态void printf(对象…o){System.out.printf(o);}
不起作用,但这对我提供的示例System.out.printf之类的东西不起作用。但对我提供的示例System.out.printf之类的东西不起作用。您提到过这适用于
System.out.printf
的特定情况。第一个参数不总是必须是字符串吗?在什么情况下它不起作用?如果参数列表是其他内容,如我在答案末尾所示,则需要访问数组的每个元素。您不能只通过
objs
@sblackYou提到这适用于
System.out.printf
的特定情况。第一个参数不总是必须是字符串吗?在什么情况下它不起作用?如果参数列表是其他内容,如我在答案末尾所示,则需要访问数组的每个元素。您不能只通过
objs
@sblackI不需要python的帮助。我想要java版的python代码,我不需要python的帮助。我想要python代码的java等价物。