Common Ancestor to Java Array and List

后端 未结 6 1948
闹比i
闹比i 2020-12-18 19:54

In .NET, both array and list have Enumerable as ancestor, so a method that accept Enumerable as an argument can receive both array and list as its argument. I wonder if ther

6条回答
  •  失恋的感觉
    2020-12-18 20:17

    They don't have a common ancestor, however, there are methods to cast between the two types as needed -

    • List.toArray()
    • Arrays.asList()

    So you could provide an overloaded method to cast to a common type - i.e.

    public void doAll(MyType[] array) {
        doAll(Arrays.asList(array));
    }
    
    public void doAll(List list) {
        //... process List here.
    }
    

提交回复
热议问题