List 与 ArrayList
- List 是一个接口,而 ArrayList 是一个类。
- List 继承了 Collection 接口
- ArrayList 实现了 List 接口
常用方法
add(int index, Object obj)
是向指定索引位置添加对象set(int index, Object obj)
是修改指定索引位置的对象
遍历元素
String a = "A", b = "B", c = "C";
List<String> list = new ArrayList<String>();
list.add(a);
list.add(b);
list.add(c);
// 遍历元素
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
除此遍历方法之外,由于 List 集合可以通过索引位置访问元素,因此还可以通过 for 循环遍历集合中元素
String a = "A", b = "B", c = "C";
List<String> list = new ArrayList<String>();
list.add(a);
list.add(b);
list.add(c);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
ArrayList 与 Vector 的区别
ArrayList 不是线程安全的,而 Vector 是线程安全的。
FEATURED TAGS
algorithm
algorithmproblem
architecturalpattern
architecture
aws
c#
cachesystem
codis
compile
concurrentcontrol
database
dataformat
datastructure
debug
design
designpattern
distributedsystem
django
docker
domain
engineering
freebsd
git
golang
grafana
hackintosh
hadoop
hardware
hexo
http
hugo
ios
iot
java
javaee
javascript
kafka
kubernetes
linux
linuxcommand
linuxio
lock
macos
markdown
microservices
mysql
nas
network
networkprogramming
nginx
node.js
npm
oop
openwrt
operatingsystem
padavan
performance
programming
prometheus
protobuf
python
redis
router
security
shell
software testing
spring
sql
systemdesign
truenas
ubuntu
vmware
vpn
windows
wmware
wordpress
xml
zookeeper