ArrayList 与 List 关系与代码示例 - Java
关系
List 是 Java Interface, ArrayList 是 Java Class,它们都属于 java.util 包。
Java List 是有序的集合(ordered collection),也称为序列(Sequence);
Java ArrayList 是 Java List Interface 的可变大小的数组实现。
这两者在 JAVA collection framework 位置,如下图所示:
再来看看官方文档说明:
java.util
Interface List<E>
- Type Parameters:
E - the type of elements in this list
All Superinterfaces:
Collection<E>, Iterable<E>
All Known Implementing Classes:
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
public interface List<E>
extends Collection<E>
An ordered collection (also known as a sequence).
java.util
Class ArrayList<E>
- java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractList<E>
- java.util.ArrayList<E>
- java.util.AbstractList<E>
- java.util.AbstractCollection<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
Direct Known Subclasses:
AttributeList, RoleList, RoleUnresolvedList
public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable
Resizable-array implementation of the List interface.
示例
1. 新建一个 ArrayList 对象
List<String> brands = new ArrayList<String>();
2. 向其中增加元素
brands.add("Jack Amber");
3. 完整示例
// BeerExpert.java, excerpted from Dawn Griffiths & David Griffiths, "Head First Android Development", 2015
import java.util.ArrayList;
import java.util.List; public class BeerExpert {
List<String> getBrands (String color) {
List<String> brands = new ArrayList<String>();
if (color.equals("amber")) {
brands.add("Jack Amber");
brands.add("Red Moose");
} else {
brands.add("Jail Pale Ale");
brands.add("Gout Stout");
} return brands;
}
}
参考资料
[1] Oracle Corporation. Class ArrayList<E> - Java™ Platform Standard Ed. 8 [OL]. https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
[2] Oracle Corporation. Interface List<E> - Java™ Platform Standard Ed. 8 [OL]. https://docs.oracle.com/javase/8/docs/api/java/util/List.html
[3] Stack Overflow Users. Type List vs type ArrayList in Java [OL]. https://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java
最新文章
- 如何给不支持新特性的浏览器打补丁(让老版本IE兼容新特性)
- ASP.NET MVC请求处理管道生命周期的19个关键环节(1-6)
- Spring Boot工程发布到Docker
- 《ASP.NET1200例》当前上下文中不存在名称configurationmanager
- android 单独编译某个模块
- SQL Server 127个SQL server热门资料汇总
- word-wrap同word-break的区别(转)
- java学习面向对象之多态
- 统计单词频率--map
- html 压缩工具 html-minifier
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
- python迭代和切片
- socket通信如何处理每次包长度不定问题
- vue实战记录(二)- vue实现购物车功能之创建vue实例
- mongodb集群配置副本集
- scala学习——(1)scala基础(上)
- cocos2d-x 真正的定时器之schedule
- linux下yum命令出现Loaded plugins: fastestmirror Determining fastest mirrors
- RocketMQ 2主2从 集群搭建
- 批量读取文件matlab