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