(1) List<?> myList = new ArrayList<?>();(2) ArrayList<?> myList = new ArrayList<?>();
use (2) if code is the "owner" of the list. This is for example true for local-only variables. There is no reason to use the abstract type List instead of ArrayList. Another example to demonstrate ownership:public class Test {    // This object is the owner of strings, so use the concrete type.
    private final ArrayList<String> strings = new ArrayList<>();    // This object uses the argument but doesn't own it, so use abstract type.
    public void addStrings(List<String> add) {
        strings.addAll(add);
    }    // Here we return the list but we do not give ownership away, so use abstract type. This also allows to create optionally an unmodifiable list.
    public List<String> getStrings() {
        return Collections.unmodifiableList(strings);
    }    // Here we create a new list and give ownership to the caller. Use concrete type.
    public ArrayList<String> getStringsCopy() {
        return new ArrayList<>(strings);
    }
}

解决方案 »

  1.   

    在我看的程序里,看到了
    ArrayList<String> creditCardYears = new ArrayList<String>();
      

  2.   

    我比较常用的是List<String> creditCardYears = new ArrayList<String>();
      

  3.   

    List<T> XXX = new ArrayList<T>();List是一个接口吧,而arraylits是实现了这个接口的一个类 
      

  4.   

    区别在于是不是需要使用ArrayList独有的方法
      

  5.   

    如果这样写:
    List<T> XXX = new ArrayList<T>();
    就不可以使用ArrayList独有的方法,对吗?