Java List vs ArrayList [英] Java List vs ArrayList

查看:97
本文介绍了Java List vs ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个C ++老朋友,我已经设法解决了我的问题,但是我无法在此处将底层的Java机制包装起来:

 矢量<对象> x = new Vector< Object>(); // OK 
ArrayList< Object> y = new ArrayList< Object>(); // OK
List< Object> zzz = new ArrayList< Object>(); //好的解决了下面的问题,但问题依然存在
List< Object> z = new List< Object>(); //为什么?编译器错误:无法实例化


解决方案

List 是一个接口,有点像C ++中一些带有 = 0 方法的类。
$ b

ArrayList< T> inherits List< ; T> (或用Java术语来实现它),所以这些引用是赋值兼容的。


As a C++ oldtimer I have managed to solve my problem but I can not wrap my head around the underlying Java mechanisms here:

Vector<Object> x = new Vector<Object>();        // OK
ArrayList<Object> y = new ArrayList<Object>();  // OK
List<Object> zzz = new ArrayList<Object>();     // OK solves problem below but question remains
List<Object> z = new List<Object>();            // WHY? Compiler error: Cannot instantiate 

解决方案

List is an interface, somewhat like a class with some = 0 methods in C++. You can't instantiate it.

But ArrayList<T> "inherits" List<T> (or in Java terms, implements it), so those references are assignment-compatible.

这篇关于Java List vs ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆