day07-常用类-ArrayList-其他常用方法说明
This commit is contained in:
44
day07/src/com/inmind/arraylist03/Demo08.java
Normal file
44
day07/src/com/inmind/arraylist03/Demo08.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package com.inmind.arraylist03;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/*
|
||||||
|
常用类_ArrayList_其他常用方法说明
|
||||||
|
对于元素的操作,基本体现在——增、删、修、查。
|
||||||
|
常用的方法有
|
||||||
|
添加:
|
||||||
|
void add(int index, E element) 在此列表中的指定位置插入指定的元素。
|
||||||
|
boolean add(E e) 将指定的元素追加到此列表的末尾。
|
||||||
|
删除:
|
||||||
|
E remove(int index) 删除该列表中指定位置的元素。
|
||||||
|
boolean remove(Object o) 从列表中删除指定元素的第一个出现(如果存在)。
|
||||||
|
修改:
|
||||||
|
E set(int index, E element) 用指定的元素替换此列表中指定位置的元素。
|
||||||
|
查询:
|
||||||
|
E get(int index) 返回此列表中指定位置的元素。
|
||||||
|
|
||||||
|
集合的长度
|
||||||
|
int size() 返回此列表中的元素数。
|
||||||
|
*/
|
||||||
|
public class Demo08 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建一个保存字符串的集合
|
||||||
|
ArrayList<String> lists = new ArrayList<>();
|
||||||
|
/*
|
||||||
|
添加:
|
||||||
|
void add(int index, E element) 在此列表中的指定位置插入指定的元素。 插队
|
||||||
|
boolean add(E e) 将指定的元素追加到此列表的末尾。 排队
|
||||||
|
*/
|
||||||
|
lists.add("柳岩");
|
||||||
|
lists.add("迪丽热巴");
|
||||||
|
lists.add("白鹿");
|
||||||
|
lists.add("杨幂");
|
||||||
|
System.out.println(lists);
|
||||||
|
System.out.println(lists.size());
|
||||||
|
//添加王宝强在白鹿之前
|
||||||
|
lists.add(2, "王宝强");
|
||||||
|
System.out.println(lists);
|
||||||
|
System.out.println(lists.size());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user