day07-常用类-ArrayList-其他常用方法说明2
This commit is contained in:
@@ -21,7 +21,87 @@ import java.util.ArrayList;
|
|||||||
int size() 返回此列表中的元素数。
|
int size() 返回此列表中的元素数。
|
||||||
*/
|
*/
|
||||||
public class Demo08 {
|
public class Demo08 {
|
||||||
|
//集合的修改
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
//创建一个保存字符串的集合
|
||||||
|
ArrayList<String> lists = new ArrayList<>();
|
||||||
|
lists.add("柳岩");
|
||||||
|
lists.add("迪丽热巴");
|
||||||
|
lists.add("王宝强");
|
||||||
|
lists.add("杨幂");
|
||||||
|
System.out.println(lists);
|
||||||
|
/*
|
||||||
|
修改:
|
||||||
|
E set(int index, E element) 用指定的元素替换此列表中指定位置的元素。
|
||||||
|
*/
|
||||||
|
//请将王宝强修改为“王宝宝”
|
||||||
|
String reVal = lists.set(2, "王宝宝");
|
||||||
|
System.out.println(reVal);
|
||||||
|
System.out.println(lists);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//集合的删除操作
|
||||||
|
public static void remove(String[] args) {
|
||||||
|
//创建一个保存字符串的集合
|
||||||
|
ArrayList<String> lists = new ArrayList<>();
|
||||||
|
|
||||||
|
lists.add("柳岩");
|
||||||
|
lists.add("迪丽热巴");
|
||||||
|
lists.add("白鹿");
|
||||||
|
lists.add("杨幂");
|
||||||
|
System.out.println(lists);
|
||||||
|
/*
|
||||||
|
删除:
|
||||||
|
E remove(int index) 删除该列表中指定位置的元素。
|
||||||
|
boolean remove(Object o) 从列表中删除指定元素的第一个出现(如果存在)。
|
||||||
|
*/
|
||||||
|
//删除索引为1的数据
|
||||||
|
String removeResult = lists.remove(1);
|
||||||
|
System.out.println(removeResult);
|
||||||
|
System.out.println(lists);
|
||||||
|
System.out.println("----------------");
|
||||||
|
//删除王宝强
|
||||||
|
boolean isSuccess = lists.remove("王宝强");
|
||||||
|
System.out.println(isSuccess);
|
||||||
|
System.out.println(lists);
|
||||||
|
System.out.println("----------------");
|
||||||
|
//删除白鹿
|
||||||
|
isSuccess = lists.remove("白鹿");
|
||||||
|
System.out.println(isSuccess);
|
||||||
|
System.out.println(lists);
|
||||||
|
}
|
||||||
|
|
||||||
|
//集合的查询
|
||||||
|
public static void get(String[] args) {
|
||||||
|
//创建一个保存字符串的集合
|
||||||
|
ArrayList<String> lists = new ArrayList<>();
|
||||||
|
|
||||||
|
lists.add("柳岩");
|
||||||
|
lists.add("迪丽热巴");
|
||||||
|
lists.add("白鹿");
|
||||||
|
lists.add("杨幂");
|
||||||
|
/*
|
||||||
|
查询:
|
||||||
|
E get(int index) 返回此列表中指定位置的元素
|
||||||
|
*/
|
||||||
|
String str1 = lists.get(0);
|
||||||
|
System.out.println(str1);
|
||||||
|
System.out.println(lists.get(1));
|
||||||
|
System.out.println(lists.get(2));
|
||||||
|
System.out.println(lists.get(3));
|
||||||
|
System.out.println("-----------------------");
|
||||||
|
//获取集合中每个元素,应该怎么办??for循环:集合的遍历
|
||||||
|
for (int i = 0; i < lists.size(); i++) {
|
||||||
|
System.out.println(lists.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//集合的添加
|
||||||
|
public static void add(String[] args) {
|
||||||
//创建一个保存字符串的集合
|
//创建一个保存字符串的集合
|
||||||
ArrayList<String> lists = new ArrayList<>();
|
ArrayList<String> lists = new ArrayList<>();
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user