进阶day02-List接口的常用方法
This commit is contained in:
41
javaSE-day03/src/com/inmind/list01/ListDemo02.java
Normal file
41
javaSE-day03/src/com/inmind/list01/ListDemo02.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package com.inmind.list01;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/*
|
||||||
|
2.List接口的常用方法
|
||||||
|
List接口是Collection接口的子接口,所以它拥有所有的Collection接口的功能,它拥有自己独有的功能,索引相关的增删改查
|
||||||
|
|
||||||
|
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 ListDemo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建List集合
|
||||||
|
List<String> lists = new ArrayList<>();
|
||||||
|
lists.add("刘备");
|
||||||
|
lists.add("关羽");
|
||||||
|
lists.add("张飞");
|
||||||
|
|
||||||
|
//独有跟索引相关的方法
|
||||||
|
//public void add(int index, E element) : 将指定的元素,添加到该集合中的指定位置上
|
||||||
|
lists.add(2, "张三");
|
||||||
|
System.out.println(lists);
|
||||||
|
|
||||||
|
//public E get(int index) :返回集合中指定位置的元素。
|
||||||
|
System.out.println(lists.get(2));//张三
|
||||||
|
|
||||||
|
//public E remove(int index) : 移除列表中指定位置的元素, 返回的是被移除的元素
|
||||||
|
System.out.println(lists.remove(2));
|
||||||
|
System.out.println(lists);
|
||||||
|
|
||||||
|
//public E set(int index, E element) :用指定元素替换集合中指定位置的元素,返回值的更新前的元素
|
||||||
|
lists.set(2, "张三");
|
||||||
|
System.out.println(lists);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user