2

在 java 1.7 上支持以下语法的原因是什么

List<Integer> ints=new ArrayList<>();

它提供了怎样的灵活性?如果我们希望 int 具有不同的类型,我们将显式转换它,如果无法执行转换,它将引发错误。

我也不清楚这是如何工作的。由于 List 是一个接口,我们如何能够在其上调用方法

List<Integer> ints = Arrays.asList(1,2);
ints.get(1);

由于返回类型asList()static<T> List<T>可以访问静态接口的字段而没有任何类提供它的实现。但是我们如何能够访问这样的接口上的方法集合库有很多Collections.Sort() 像谁提供了实现这些方法?

4

6 回答 6

4

问题 1

使用

List<Integer> ints=new ArrayList<>();

代替

List<Integer> ints=new ArrayList<Integer>();

不会增加任何灵活性,因为它们完全相同。菱形 ( <>) 表示我们不需要原始类型,而是明显的参数化类型。

甲骨文在这里描述它:

在 Java SE 7 中,您可以用一组空的类型参数 (<>) 替换构造函数的参数化类型:

它只是使代码不那么冗长,因此更易于阅读和维护。这在 Oracle 的示例中可能更为明显:

Map<String, List<String>> myMap = new HashMap<>();

相比

Map<String, List<String>> myMap = new HashMap<String, List<String>>();

问题2

Arrays.asList(1,2);从实现 List 接口的具体类返回一个对象。

在 Java 1.6 中,它是一个固定大小的实例java.util.Arrays.ArrayList

3354    public static <T> List<T> asList(T... a) {
3355        return new ArrayList<T>(a);
3356    }

但重要的一点是,对象总是属于实现其接口定义的方法的具体类。

当你这样做的时候Collections.sort(,你不是在使用Collection接口,而是使用包含这个非常具体的代码的Collections类:

132     public static <T extends Comparable<? super T>> void sort(List<T> list) {
133         Object[] a = list.toArray();
134         Arrays.sort(a);
135         ListIterator<T> i = list.listIterator();
136         for (int j=0; j<a.length; j++) {
137             i.next();
138             i.set((T)a[j]);
139         }
140     }
于 2012-11-08T11:34:03.293 回答
0

你的问题有很多问题。

泛型提供编译时检查,而直到运行时才发现转换错误。

您的 ints 是一个支持 List 接口的对象,因此在其上调用方法是有效的。

于 2012-11-08T11:35:44.097 回答
0

这些是编译时检查,旨在确保一致性,因此实现不必“做”任何特定的事情,只要它们遵循泛型符号规定的规则(例如,对于使用类型 T 的对象调用的方法) ,你只返回同样类型的东西 T)。

正如已经指出的那样,当很明显在构造函数中指定类型是不必要的冗长时,引入了菱形表示法 - 可以从构造函数。

于 2012-11-08T11:37:37.307 回答
0
List<Integer> ints=new ArrayList<>();

它的泛型易于理解和迭代

for(Integer intObj : ints ) {}

代替

Iterator itr = ints.iterator() ;
while(itr.hasNext()) {
   Integer intObj = (Integer) itr.next();
}

由于返回类型Arrays.asList()是静态的?

java.util.Arrays的,一个 util 类提供了很多实用方法。

但是我们如何能够访问这样一个接口上的方法呢?

只需查看那里的源代码,您就可以从接口ArrayList中找到实现方法。List

谁提供这些方法的实现?

实现接口的人List Set

于 2012-11-08T11:49:54.503 回答
0
List<Integer> ints=new ArrayList<>();

和写完全一样

List<Integer> ints=new ArrayList<Integer>();

It doesn't add flexibility, it just let you keep generics with your ArrayList whithout having to retype the type of the ArrayList, and so the ArrayList will not use raw type.

Some other thing is that writing this, you only get directly access to the List interface methods on your ArrayList object, event if these methods will have the behavior that is defined in the ArrayList class.

于 2012-11-08T12:00:22.740 回答
0

What flexibility does it provide?

It does not provide any flexibility; is just a syntactic sugaring. Kind of pointless since modern IDEs code completion already do that.

Since return type of asList() is static List it's okay to access a field of a static interface without any class providing an implementation of it.But how are we able to access a method on such an interface The collections library has a lot of them like Collections.Sort() Who provides the implementation of these methods?

The return type of asList() is List; the type has nothing to do with the fact that the method is static. Arrays is a class, not an interface, and asList() method is implemented there; the JDK provides the implementation.

于 2012-11-08T12:05:22.773 回答