ArrayList<E>E是自定义数据类型
ArrayList类:
构造函数:
成员方法:
public boolean add(E e):
将指定元素加到集合末尾
Appends the specified element to the end of this list.
public class Array {
public static void main(String[] args) {
ArrayList arr=new ArrayList();
arr.add("nihao");//从末尾添加
arr.add(67);
arr.add("he");
System.out.println(arr);//[nihao, 67, he]
}
}
可以指定向里面特定数据类型
public class Array {
public static void main(String[] args) {
ArrayList<String> arr=new ArrayList();
arr.add("nihao");
arr.add("eq");
System.out.println(arr);//[nihao, eq]
}
}
public void add(int index, E element)
给集合的指定位置插入指定的元素
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
public class Array {
public static void main(String[] args) {
ArrayList<String> arr=new ArrayList();
arr.add("nihao");
arr.add("eq");
System.out.println(arr);//[nihao, eq]
arr.add(2,"my");
System.out.println(arr);//[nihao, eq, my]
}
}
public E get(int index)
返回索引处的元素
Returns the element at the specified position in this list.
public class Array {
public static void main(String[] args) {
ArrayList<String> arr=new ArrayList();
arr.add("nihao");
arr.add("eq");
System.out.println(arr);//[nihao, eq]
arr.add(2,"my");
System.out.println(arr);//[nihao, eq, my]
String el=arr.get(1);
System.out.println(el);//eq
}
}
public int size()
返回集合中的集合元素的个数
Returns the number of elements in this list.
public class Array {
public static void main(String[] args) {
ArrayList<String> arr=new ArrayList();
arr.add("nihao");
arr.add("eq");
System.out.println(arr);//[nihao, eq]
arr.add(2,"my");
System.out.println(arr);//[nihao, eq, my]
String el=arr.get(1);
System.out.println(el);//eq
int size=arr.size();
System.out.println(size);//3
}
}
public E remove(int index)
删除指定索引处的元素,返回被删除的元素
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
public class Array {
public static void main(String[] args) {
ArrayList<String> arr=new ArrayList();
arr.add("nihao");
arr.add("eq");
System.out.println(arr);//[nihao, eq]
arr.add(2,"my");
System.out.println(arr);//[nihao, eq, my]
String el=arr.get(1);
System.out.println(el);//eq
int size=arr.size();
System.out.println(size);//3
String el2=arr.remove(2);
System.out.println(el2);//my
System.out.println(arr);// [nihao, eq]
}
}
public boolean remove(Object o)
删除指定的元素,删除成功返回true,第一个出现的数据
Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i
such that Objects.equals(o, get(i))
(if such an element exists). Returns true
if this list contained the specified element (or equivalently, if this list changed as a result of the call).
public class Array {
public static void main(String[] args) {
ArrayList<String> arr=new ArrayList();
arr.add("nihao");
arr.add("eq");
System.out.println(arr);//[nihao, eq]
arr.add(2,"my");
System.out.println(arr);//[nihao, eq, my]
String el=arr.get(1);
System.out.println(el);//eq
int size=arr.size();
System.out.println(size);//3
String el2=arr.remove(2);
System.out.println(el2);//my
System.out.println(arr);// [nihao, eq]
System.out.println(arr.remove("eq"));//true
System.out.println(arr);//[nihao]
}
}
public E set(int index, E element)
修改指定索引的值
Replaces the element at the specified position in this list with the specified element.
arr.set(0,"hhh");
System.out.println(arr);//[hhh]
从集合中遍历元素,删除含有特定内容的元素,应该怎么做:
方法一:每次删除一个元素,索引-1;
方法二:从集合的最后开始向前遍历,可以避免漏掉元素
标签:arr,java,ArrayList,System,add,println,eq,out From: https://blog.csdn.net/luosuss/article/details/137055942