-1

What is the diference between using Array List and a List. Actually Java throws me an exception when I want to instantiate the List with istself, in fact I have to do it with a ArrayList.

4

4 回答 4

1

List是一种抽象类型 - java interface。接口定义了可以在实现的对象上找到哪些方法List

AnArrayListList接口的具体实现(它在内部使用数组来存储元素,因此得名)。

AnArrayList是 a List,但 aList不一定a ArrayList。还有其他实现,例如LinkedList.

参见Liskov 替换原则

于 2013-07-13T03:24:01.187 回答
0

List is an interface, ArrayList is a class that implements List. You can't instantiate an interface.

于 2013-07-13T03:07:04.030 回答
0

A List is a general type of object known as an interface. There are many types of lists including but not limited to ArrayList and LinkedList. The list interface provide handles to a set of methods required for manipulating a list without knowing what type of list it is. Any type of list can be assigned to a variable of type List. But list itself cannot be instantiated because it is only an interface and not an implementation. See the Java list tutorial for more information. http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html

于 2013-07-13T03:08:07.963 回答
0

您的代码引发异常原因List是抽象的,您无法实例化抽象的东西。

List 是一个接口。在这里,您定义了合同这种类型的行为签名。

ArrayList 是一个 List ,因为implements ListArrayList是一个具体的实现 ,所以所有的方法都由 List 接口定义。

在 Java 中,您显然可以实例化一个接口,anonymous classes 但您确实有一个具体的类来实现List无名合同(这就是匿名名称的原因)。

这是一个例子

List list = new List(){

            @Override
            public boolean add(Object arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void add(int arg0, Object arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean addAll(Collection arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean addAll(int arg0, Collection arg1) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void clear() {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean contains(Object arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean containsAll(Collection arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Object get(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public int indexOf(Object arg0) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Iterator iterator() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public int lastIndexOf(Object arg0) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public ListIterator listIterator() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public ListIterator listIterator(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean remove(Object arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Object remove(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public boolean removeAll(Collection arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean retainAll(Collection arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public Object set(int arg0, Object arg1) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public int size() {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public List subList(int arg0, int arg1) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Object[] toArray() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public Object[] toArray(Object[] arg0) {
                // TODO Auto-generated method stub
                return null;
            }

        };

正如@Bohemian 所说,如果您制作自己的列表实现,则必须注意Liskov 替换原则,不要违反此原则并遵守合同。

于 2013-07-13T03:26:46.150 回答