Java two varargs in one method

后端 未结 12 2064
小鲜肉
小鲜肉 2020-11-27 21:08

Is there any way in java, to create a method, which is expecting two different varargs? I know, with the same object kind it isn\'t possible because the compiler does\'nt kn

相关标签:
12条回答
  • 2020-11-27 22:04

    You can convert your varargs to arrays

    public void doSomething(String[] s, int[] i) {
        ...
    }
    

    then with some helper methods to convert your varargs to array like this:

    public static int[] intsAsArray(int... ints) {
        return ints;
    }
    
    public static <T> T[] asArray(T... ts) {
        return ts;
    }
    

    Then you can use those helper methods to convert your vararged parameters.

    doSomething(asArray("a", "b", "c", "d"), intsAsArray(1, 2, 3));
    
    0 讨论(0)
  • 2020-11-27 22:07

    In Java, only one varargs argument is allowed and it must be the last parameter of the signature.

    But all it does it convert it to an array anyway, so you should just make your two parameters explicit arrays:

    public void doSomething(String[] s, int[] i){
    
    0 讨论(0)
  • 2020-11-27 22:07

    A possible API design in which the calling code looks like

        doSomething("a", "b").with(1,2);
    

    through "fluent" API

    public Intermediary doSomething(String... strings)
    {
        return new Intermediary(strings);
    }
    
    class Intermediary
    {
        ...
        public void with(int... ints)
        {
            reallyDoSomething(strings, ints);
        }
    }
    
    void reallyDoSomething(String[] strings, int[] ints)
    {
        ...
    }
    

    The danger is if the programmer forgot to call with(...)

        doSomething("a", "b");  // nothing is done
    

    Maybe this is a little better

        with("a", "b").and(1, 2).doSomething();
    
    0 讨论(0)
  • 2020-11-27 22:08

    This is an old thread, but I thought this would be helpful regardless.

    The solution I found isn't very neat but it works. I created a separate class to handle the heavy lifting. It only has the two variables I needed and their getters. The constructor handles the set methods on its own.

    I needed to pass direction objects and a respective Data object. This also solves the possible problem of uneven data pairs, but that is probably only for my usage needs.

    public class DataDirectionPair{
    
        Data dat;
        Directions dir;
    
        public DataDirectionPair(Data dat, Directions dir) {
            super();
            this.dat = dat;
            this.dir = dir;
        }
    
        /**
         * @return the node
         */
        public Node getNode() {
            return node;
        }
    
        /**
         * @return the direction
         */
        public Directions getDir() {
            return dir;
        }
    }
    

    I would then just pass this class as the vararg for the method

    public void method(DataDirectionPair... ndPair){
        for(DataDirectionPair temp : ndPair){
            this.node = temp.getNode();
            this.direction = temp.getDir();
            //or use it however you want
        }
    }
    
    0 讨论(0)
  • 2020-11-27 22:10

    Although this kind of thing is occasionally useful, usually if you find that you are hitting a restriction in Java you could probably redesign something and come out much better. Here are some possible other ways to look at it...

    If the two lists are related at all you probably want to create a wrapper class for the two different lists and pass in the wrapper. Wrappers around collections are almost always a good idea--they give you a place to add code that relates to the collection.

    If this is a way to initialize data, parse it from a string. For instance, "abc, 123:def, 456:jhi,789" is almost embarassingly easy to split up with 2 split statements and a loop (2-3 lines of code). You can even make a little custom parser class that parses a string like that into a structure you feed into your method.

    Hmm--honestly asside from initializing data I don't even know why you'd want to do this anyway, any other case and I expect you'd be passing in 2 collections and wouldn't be interested in varags at all.

    0 讨论(0)
  • 2020-11-27 22:10

    You can do something like this, then you can cast and add additional logic inside that method.

    public void doSomething(Object... stringOrIntValues) {
        ...
        ...
    }
    

    And use this method like so:

    doSomething(stringValue1, stringValue2, intValue1, intValue2,         
        intValue3);
    
    0 讨论(0)
提交回复
热议问题