s-day02-List接口的常用方法
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
package com.inmind.list01;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/*
|
||||||
|
2.List接口的常用方法
|
||||||
|
List既拥有Collection的共有方法,也有它自己独有的功能,有序集合,有索引相关的增删改查操作
|
||||||
|
索引相关的增删改查API
|
||||||
|
public void add(int index, E element) : 将指定的元素,添加到该集合中的指定位置上。
|
||||||
|
public E get(int index) :返回集合中指定位置的元素。
|
||||||
|
public E remove(int index) : 移除列表中指定位置的元素, 返回的是被移除的元素。
|
||||||
|
public E set(int index, E element) :用指定元素替换集合中指定位置的元素,返回值的更新前的元素。
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//List的多态
|
||||||
|
List<String> lists = new ArrayList<>();
|
||||||
|
lists.add("刘备");
|
||||||
|
lists.add("关羽");
|
||||||
|
lists.add("张飞");
|
||||||
|
System.out.println(lists);
|
||||||
|
|
||||||
|
//吕布插入到张飞之前
|
||||||
|
lists.add(2, "吕布");
|
||||||
|
System.out.println(lists);
|
||||||
|
|
||||||
|
//获取索引为3的值
|
||||||
|
System.out.println(lists.get(3));
|
||||||
|
|
||||||
|
//将索引为2的值删除
|
||||||
|
System.out.println(lists.remove(2));
|
||||||
|
System.out.println(lists);
|
||||||
|
|
||||||
|
//把关羽修改为关二哥
|
||||||
|
System.out.println(lists.set(1, "关二哥"));//返回的是更新前的关羽
|
||||||
|
System.out.println(lists);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user