Java unpacking argument lists

后端 未结 2 1752
灰色年华
灰色年华 2021-02-20 10:40

Here\'s another question of \"How would I do this in Java?\" In Python, I can use the \'*\' symbol to unpack arguments like so:

>>> range(3, 6)                  


        
2条回答
  •  鱼传尺愫
    2021-02-20 11:14

    public void printStrings(String... strings)
    {
       // the strings parameter is really a String[].
       // You could do anything to it that you normally
       // do with an array.
       for(String s : strings){
          System.out.println(s);
       }
    }
    

    Can be called like this:

    String[] stringArray = new String[10];
    for(int i=0; i < stringArray.length; i++){
       stringArray[i] = "String number " + (i+1);
    }
    
    printStrings(stringArray);
    

    The ... syntax is really syntactic sugar for arrays.

    Java doesn't have the facility that you describe, but you could fake it several ways.

    I think the closest approximation means overloading any function that you want to use in that fashion using varargs.

    If you have some method:

    public void foo(int a, String b, Widget c) { ... }
    

    You can overload it:

    public void foo(Object... args) {
        foo((Integer)args[0], (String)args[1], (Widget)args[2]);
    }
    

    But this is really clumsy and error prone and hard to maintain.

    More generically, you could use reflection to call any method using any arguments, but it's got a ton of pitfalls, too. Here's a buggy, incomplete example of how it gets ugly really fast:

    public void call(Object targetInstance, String methodName, Object... args) {
        Class[] pTypes = new Class[args.length];
        for(int i=0; i < args.length; i++) {
            pTypes[i] = args[i].getClass();
        }
        Method targetMethod = targetInstance.getClass()
                  .getMethod(methodName, pTypes);
        targetMethod.invoke(targetInstance, args);
    }
    

提交回复
热议问题