目录
1、 boolean add() 把给定的对象添加到当前集合中。
3、 boolean remove(E e): 把给定的对象在当前集合中删除。
4、boolean contains(Object obj): 判断当前集合中是否包含给定的对象。
5、boolean isEmpty(): 判断当前集合是否为空。
7、Object[] toArray(): 把集合中的元素,存储到数组中
一、集合
1、集合概述?
集合是java中提供的一种容器,可以用来存储多个数据。
2、集合和数组的区别?
1、长度的区别
数组的长度是固定的,如:int [] x = new int[8];这个数组只能存放八个数据,
集合的长度是可变的
2、存放的数据的区别
数组可以存放基本数据类型和引用数据类型的数据
集合只能存放引用数据类型(如果要存放基本数据类型则需要用到对应的包装类)
3、集合常用类的继承体系
Collection:单列集合的根接口
java.util.list特点:元素是有序的、元素可重复
java.util.set特点:不可重复
因为是接口,所以要调用它们里面的方法就必须创建一个它们的实现类对象,之后通过这个对象来调用。Collection是所有单列集合的父接口
ArrayList<Object> List = new ArrayList<>();
list.add(1); //这里为什么不报错呢,不是说集合只能引用数据类型吗?
list.add(2); //这里其实村的是Integer对象,(自动装箱)
4、Collection集合的常用方法
因为Collection是所有单列集合的父接口,所以已下方法是List和Set以及Collection的其他子类通用的。(已下方法前面的的public省略)
1、 boolean add() 把给定的对象添加到当前集合中。
public class TestCollection {
public static void main(String[] args) {
//测试add方法
//创建一个集合
ArrayList arrayList = new ArrayList();
String str = "come的喂,ABC";
String str1 = "嘿嘿";
arrayList.add(str);
arrayList.add(str1);
}
}
这就是集合的创建以及往集合中添加元素的方法, 那我怎么直到有没有添加进去呢?继续往下看。
2、int size() 返回集合中元素的个数。
import java.util.ArrayList;
public class TestCollection {
public static void main(String[] args) {
//测试add方法
//创建一个集合
ArrayList arrayList = new ArrayList();
//输出集合的长度
System.out.println("添加元素前集合的长度为:"+arrayList.size());
String str = "come的喂,ABC";
String str1 = "嘿嘿";
arrayList.add(str);
arrayList.add(str1);
System.out.println("添加元素后集合的长度为:"+arrayList.size());
}
}
运行结果:
这就是Collection里面的size方法,调用了它之后就能得到集合的长度了,也就是集合里元素的个数,添加前为0,添加后为2,成功添加两个元素到集合里面了。
3、 boolean remove(E e): 把给定的对象在当前集合中删除。
import java.util.ArrayList;
public class TestCollection {
public static void main(String[] args) {
//测试add方法
//创建一个集合
ArrayList arrayList = new ArrayList();
//输出集合的长度
System.out.println("添加元素前集合的长度为:"+arrayList.size());
String str = "come的喂,ABC";
String str1 = "嘿嘿";
arrayList.add(str);
arrayList.add(str1);
System.out.println("添加元素后集合的长度为:"+arrayList.size());
arrayList.remove("嘿嘿");
System.out.println("删除元素后集合的长度为:"+arrayList.size());
}
}
运行结果:
remove方法需要传入一个对象,我们开始存了字符串对象在集合里面,那么现在就传入一个字符串对象到remove里面,之后这个元素就被删除了,集合的长度变为1了,删除成功。
4、boolean contains(Object obj): 判断当前集合中是否包含给定的对象。
import java.util.ArrayList;
public class TestCollection {
public static void main(String[] args) {
//测试add方法
//创建一个集合
ArrayList arrayList = new ArrayList();
//输出集合的长度
System.out.println("添加元素前集合的长度为:"+arrayList.size());
String str = "come的喂,ABC";
String str1 = "嘿嘿";
arrayList.add(str);
arrayList.add(str1);
System.out.println("添加元素后集合的长度为:"+arrayList.size());
arrayList.remove("嘿嘿");
System.out.println("删除元素后集合的长度为:"+arrayList.size());
//contains方法测试
String str2 = "cgfvh";
System.out.println(str2+(arrayList.contains(str2)?"在集合里面":"不在集合里面"));
}
}
运行结果:
很明显,集合里面只有一个"come的喂,ABC"元素了,再看改成有的情况
这时就会输出在集合里面了。
5、boolean isEmpty(): 判断当前集合是否为空。
public class TestCollection {
public static void main(String[] args) {
//测试add方法
//创建一个集合
ArrayList arrayList = new ArrayList();
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
//输出集合的长度
System.out.println("添加元素前集合的长度为:"+arrayList.size());
System.out.println("此处添加了两个元素");
String str = "come的喂,ABC";
String str1 = "嘿嘿";
arrayList.add(str);
arrayList.add(str1);
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
System.out.println("添加元素后集合的长度为:"+arrayList.size());
arrayList.remove("嘿嘿");
System.out.println("删除元素后集合的长度为:"+arrayList.size());
//contains方法测试
String str2 = "come的喂,ABC";
System.out.println(str2+(arrayList.contains(str2)?"在集合里面":"不在集合里面"));
}
}
运行结果:
6、 void clean() 清空集合中所有的元素。
import java.util.ArrayList;
public class TestCollection {
public static void main(String[] args) {
//测试add方法
//创建一个集合
ArrayList arrayList = new ArrayList();
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
//输出集合的长度
System.out.println("添加元素前集合的长度为:"+arrayList.size());
System.out.println("此处添加了两个元素");
String str = "come的喂,ABC";
String str1 = "嘿嘿";
arrayList.add(str);
arrayList.add(str1);
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
System.out.println("添加元素后集合的长度为:"+arrayList.size());
arrayList.remove("嘿嘿");
System.out.println("删除元素后集合的长度为:"+arrayList.size());
//contains方法测试
String str2 = "come的喂,ABC";
System.out.println(str2+(arrayList.contains(str2)?"在集合里面":"不在集合里面"));
//清空数组里面所有的元素
arrayList.clear();
System.out.println("清空集合里面所有的元素了");
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
}
}
运行结果:
7、Object[] toArray(): 把集合中的元素,存储到数组中
import java.util.ArrayList;
public class TestCollection {
public static void main(String[] args) {
//测试add方法
//创建一个集合
ArrayList arrayList = new ArrayList();
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
//输出集合的长度
System.out.println("添加元素前集合的长度为:"+arrayList.size());
System.out.println("此处添加了两个元素");
String str = "come的喂,ABC";
String str1 = "嘿嘿";
arrayList.add(str);
arrayList.add(str1);
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
System.out.println("添加元素后集合的长度为:"+arrayList.size());
arrayList.remove("嘿嘿");
System.out.println("删除元素后集合的长度为:"+arrayList.size());
//contains方法测试
String str2 = "come的喂,ABC";
System.out.println(str2+(arrayList.contains(str2)?"在集合里面":"不在集合里面"));
//清空数组里面所有的元素
arrayList.clear();
System.out.println("清空集合里面所有的元素了");
System.out.println(arrayList.isEmpty()?"数组为空":"数组不为空");
//测试toArray方法
arrayList.add(str);
arrayList.add(str1);
arrayList.add(str2);
Object[] strArr = arrayList.toArray();
for (Object item: strArr){
System.out.print(item+"\t");
}
}
}
运行结果:
最后一行输出了集合里面所有的元素。
二、Iterator迭代器
1、迭代器的概述
Iterator对象称作为迭代器,用以方便的对容器内元素的遍历操作,Iterator接口定义了如下方法:
boolean hashNext(); //判断是否有元素没有被遍历
Object.next(0; //返回游标当前位置的元素并将游标移动到下一个位置
void remove();// 删除游标左边的元素,在执行完next之后该操作只能执行一次。
2、迭代器的获取
集合提供了方法可以直接获取迭代器。
Iterable<E> 是Collection的父接口,提供了一个方法:
Iterator<E> iterator():专门用来获取迭代器对象的
//意味着Collection集合都有迭代的能够力。
注意两个接口的区别:
Iterable:可以迭代,表示一种可迭代的能力(提供了获取迭代器的方法)
Iterator:迭代器具体实现迭代功能(真正的迭代器)
3、迭代器的使用
使用步骤:
1. 获取迭代器: iterator
2. 判断是否存在下一个元素: hasNext
3. 如果存在获取下一个元素: next
4. 重复2,3步骤直到没有下一个元素
例:
import java.util.Iterator;
import java.util.LinkedList;
public class TestCollection {
public static void main(String[] args) {
String[] strs = {"12345", "67891", "12347809933", "98765432102", "67891", "12347809933"};
//1、创建集合
LinkedList<String> list = new LinkedList<>();
//遍历字符串,把字符串对象添加到集合中
for (String str : strs) {
list.add(str);
}
System.out.println("使用迭代器输出List里面的数据");
//获取迭代器对象
Iterator<String> iter1 = list.iterator();
while (iter1.hasNext()) {
System.out.print(iter1.next() + "\t");
}
}
}
4、增强型for循环
1、概述
增强for循环(也称for each循环)是**JDK1.5**以后出来的一个高级for循环,专门用来遍历数组和集合的。它的内部原理其实是个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行增删操作。
2、使用方法
import java.util.LinkedList;
public class TestCollection {
public static void main(String[] args) {
String[] strs = {"12345", "67891", "12347809933", "98765432102", "67891", "12347809933"};
//1、创建集合
LinkedList<String> list = new LinkedList<>();
//遍历字符串,把字符串对象添加到集合中
for (String str : strs) {
list.add(str);
}
System.out.println("使用增强型for循环输出List里面的数据");
for (Object item : list) {
System.out.print(item + "\t");
}
}
}