Collection与它的儿子ArrayList
- 一、Collection接口
- CollectionDemo
- 1、public boolean add(E e):把给定的对象添加到当前集合中,返回值是一个boolean值,一般返回true,可以不用接收
- 2、public boolean remove(E e):把给定的对象在当前集合中删除返回值是一个boolean值。集合中存在该元素,会删除该元素,返回true;集合中不存在该元素,删除失败,返回false
- 3、public boolean contains(E e):判断当前集合中是否包含给定的对象返回值是一个boolean值。集合中包含该元素,返回true;集合中不包含该元素,返回false
- 4、public boolean isEmpty( ):判断当前集合是否为空返回值是一个boolean值。集合为空,返回true;集合不为空,返回false
- 5、public int size( ):返回集合中元素的个数
- 6、public Object[] toArray( ):把集合中的元素,存储到数组中
- 7、public void clear( ):清空集合中所有元素,但不删除集合,集合还存在
- 二、List接口
- 三、总结
一、Collection接口
Collection是所有单列集合最顶层的接口,因此在Collection中定义了单列集合(List和Set)通用的一些方法,这些方法可以用于操作所有单列集合。
通用的(共性)方法eg:
1、public boolean add(E e):把给定的对象添加到当前集合中
2、public boolean remove(E e):把给定的对象在当前集合中删除
3、public boolean contains(E e):判断当前集合中是否包含给定的对象
4、public boolean isEmpty( ):判断当前集合是否为空
5、public int size( ):返回集合中元素的个数
6、public Object[] toArray( ):把集合中的元素,存储到数组中
7、public void clear( ):清空集合中所有元素
CollectionDemo
先创建一个对象coll
public static void main(String[] args) {
//创建集合对象,可以使用多态
Collection<String> coll = new ArrayList<>();
System.out.println(coll); // [] ,重写了toString方法
System.out.println("---------");
运行结果
1、public boolean add(E e):把给定的对象添加到当前集合中,返回值是一个boolean值,一般返回true,可以不用接收
代码如下:
boolean c1 = coll.add("以太不闪火");
System.out.println("c1:" + c1); //c1:true
System.out.println(coll); //[以太不闪火]
//添加其他元素
coll.add("Ugly Z");
coll.add("Pg One");
coll.add("GAI");
System.out.println(coll); //[以太不闪火, Ugly Z, Pg One, GAI]
System.out.println("---------");
运行结果
2、public boolean remove(E e):把给定的对象在当前集合中删除返回值是一个boolean值。集合中存在该元素,会删除该元素,返回true;集合中不存在该元素,删除失败,返回false
代码如下:
boolean c2 = coll.remove("GAI"); //删除集合中的对象元素
System.out.println("c2:" + c2); //true
System.out.println(coll); //[以太不闪火, Ugly Z, Pg One]
boolean c3 = coll.remove("GAI"); //删除集合中没有的元素
System.out.println("c3:" + c3); //false
System.out.println("---------");
运行结果
3、public boolean contains(E e):判断当前集合中是否包含给定的对象返回值是一个boolean值。集合中包含该元素,返回true;集合中不包含该元素,返回false
代码如下:
boolean c4 = coll.contains("Pg One"); //集合中包含该对象元素
System.out.println("c4:" + c4); //true
boolean c5 = coll.contains("GAI"); //集合中不包含该对象元素
System.out.println("c5:" + c5); //false
System.out.println("---------");
运行结果
4、public boolean isEmpty( ):判断当前集合是否为空返回值是一个boolean值。集合为空,返回true;集合不为空,返回false
代码如下:
boolean c6 = coll.isEmpty(); //集合不为空
System.out.println("c6:" + c6); //false
System.out.println("---------");
运行结果
5、public int size( ):返回集合中元素的个数
代码如下:
int collSize = coll.size();
System.out.println("集合中元素个数为:" + collSize); //3
System.out.println("---------");
运行结果
6、public Object[] toArray( ):把集合中的元素,存储到数组中
代码如下:
Object[] arr = coll.toArray(); //快捷键:集合名称.toArray().var
for (int i = 0; i < arr.length; i++) { //快捷键:数组名.fori
System.out.println(arr[i]);
}
System.out.println("---------");
运行结果
7、public void clear( ):清空集合中所有元素,但不删除集合,集合还存在
代码如下:
coll.clear(); //清空集合
System.out.println(coll); //[ ]
System.out.println(coll.isEmpty()); //true,集合为空
运行结果
二、List接口
List继承Collection接口,是有序的collection,也称‘序列’。List接口的用户可以对列表
中每个元素的插入位置竞选精确控制,可以根据元素的整数索引访问元素,并搜索列表中的元素
特点:
1.有序的集合,存储元素和取出元素的顺序是一致的 (存123 取123)
2.有索引,包含一些带索引的方法
3.允许存储重复元素
List接口中带索引的方法(特有):
1.public void add(int index, E element):将指定的元素,添加到集合中的指定位置
2.public E remove(int index):移除列表中指定位置的元素,返回被移除的元素
3.public E set(int index, E element):用指定元素替换集合中指定位置的元素,返回值的更新前的元素
4.public E get(int index):返回集合中指定位置的元素
List集合的3种遍历方式:
1.使用for循环
2.使用迭代器
3.使用增强for
注意: 操作索引的时候,一定要防止索引越界异常
ListDemo
先创建一个对象list
public static void main(String[] args) {
//创建一个list集合对象,多态
List<String> list = new ArrayList<>();
//使用add方法,在集合中添加元素
list.add("土豆"); //[0]
list.add("洋芋"); //[1]
list.add("地瓜"); //[2]
list.add("马铃薯"); //[3]
list.add("土豆"); //[4]
System.out.println("集合1.0版本:" + list); //[土豆, 洋芋, 地瓜, 马铃薯, 土豆] 重写了toString方法
System.out.println("------------------------------------------------------------");
运行结果
1.public void add(int index, E element):将指定的元素,添加到集合中的指定位置
代码如下:
list.add(3, "我是炸洋芋"); //索引3添加新元素
System.out.println("添加后的集合2.0版本:" + list); //[土豆, 洋芋, 地瓜, 我是炸洋芋, 马铃薯, 土豆]
// [0] [1] [2] [3] [4] [5]
System.out.println("------------------------------------------------------------");
运行结果
2.public E remove(int index):移除列表中指定位置的元素,返回被移除的元素
代码如下:
String removeS = list.remove(3); //移除索引3元素,
System.out.println("移除索引3的元素:" + removeS); //我是炸洋芋,返回被移除的元素
System.out.println("移除后的集合3.0版本:"+list); //[土豆, 洋芋, 地瓜, 马铃薯, 土豆]
System.out.println("------------------------------------------------------------");
运行结果
3.public E set(int index, E element):用指定元素替换集合中指定位置的元素,返回值的更新前的元素
代码如下:
String setS = list.set(4,"炸洋芋回来了"); //"炸洋芋回来了"替换索引为4的元素
System.out.println("索引4被替换的元素:"+setS); //土豆,返回被替换的元素
System.out.println("替换后的集合4.0版本:"+list); //土豆, 洋芋, 地瓜, 马铃薯, 炸洋芋回来了]
System.out.println("------------------------------------------------------------");
运行结果
4.public E get(int index):返回集合中指定位置的元素
代码如下:
String getS =list.get(3); //获取索引为3的元素
System.out.println("获取索引为3的元素:"+getS); //马铃薯
System.out.println("------------------------------------------------------------");
运行结果
List集合遍历
/*--遍历--*/
//1.for循环
for (int i = 0; i < list.size(); i++) {
System.out.println("for循环遍历:"+list.get(i));
}
System.out.println("------------------------------------------------------------");
//2.迭代器
Iterator<String> it = list.iterator(); //快捷键:集合名.iterator().var
while (it.hasNext()){
System.out.println("迭代器遍历:"+it.next());
}
System.out.println("------------------------------------------------------------");
//3.增强for
for (String s :list){
System.out.println("增强for遍历:"+s);
}
运行结果
三、总结
以上是学习笔记分享,感觉不是太难,但是要会运用到实际的代码中才是真的学会了。对于集合,学习顶层接口或抽象类中共性的方法,子类都可以用;使用底层,不管是接口还是抽象类,都无法创建对象使用,需要使用底层的子类创建对象使用。