List(动态数组)存储特点:有序、可重复
Collection中的14个方法
因为List是有序的进而就有索引,进而就有一些针对索引的操作
•插入元素
方法 | 作用 |
---|---|
–void add(int index, Object ele) | 在index位置插入ele元素 |
–boolean addAll(int index, Collection eles) | 从index位置开始将eles中的所有元素添加进来 |
•获取元素
方法 | 作用 |
---|---|
–Object get(int index) | 获取指定index位置的元素 |
–List subList(int fromIndex, int toIndex) | 返回从fromIndex到toIndex位置的子集合 |
•获取元素索引
方法 | 作用 |
---|---|
–int indexOf(Object obj) | 返回obj在集合中首次出现的位置 |
–int lastIndexOf(Object obj) | 返回obj在当前集合中末次出现的位置 |
•删除和替换元素
方法 | 作用 |
---|---|
–Object remove(int index) | 移除指定index位置的元素,并返回此元素 |
–Object set(int index, Object ele) | 设置指定index位置的元素为ele |
增:
add(Object o)、addAll(Collection c)
删:
remove(Object o)、remove(int index)
改:
set(int index,Object o)
查:
get(int index)
插:
add(int index,Object o)、addAll(int index,Collection c)
长度:
siez()
遍历:
iterator():使用迭代器遍历
foreach:增强for循环
for:一般for循环
public class ListTest {
public static void main(String[] args) {
ArrayList al=new ArrayList();
al.add("AA");
al.add("AA");
al.add(123);
al.add(new Product("手机",1499));
System.out.println(al);
//add
al.add(2,"CC");
System.out.println(al);
//remove删除索引
al.remove(2);
System.out.println(al);
//删除数据 123
al.remove(Integer.valueOf(123));
System.out.println(al);
//迭代器遍历
Iterator iterator = al.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//foreach遍历
for (Object o:al) {
System.out.println(o);
}
//for遍历
for(int i=0;i<al.size();i++){
System.out.println(al.get(i));
}
}
}
推荐阅读:
vue.js:634 [Vue warn]: Property or method "userName" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See:
状态码:415,发送HTTP请求返回415状态码的解决办法
SpringBoot 整合 webapp时 访问404的解决办法
Vue 打开页面时就加载方法,例如查询
SpringCloud怎么调用多个服务的信息
Spring整合Mybatis
Controller之间的跳转
SpringCloud 加入 thymleaf前端页面的方法
Spring AOP的实现原理
This may be the result of an unspecified view, due to default view name generation
Seek respect, not attention.