Common Ancestor to Java Array and List

后端 未结 6 1960
闹比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:23

    Array and List in Java do not share a common ancestor other than java.lang.Object.

    Both can be accessed using the foreach loop, like so:

    String [] array = new String [] { "foo", "bar", "baz", };
    List list = Arrays.asList( "x", "y", "z");
    
    for (String s : array)
        System.out.println(s);
    
    for (String s : list)
        System.out.println(s);
    

提交回复
热议问题