集合类
集合类:集合可以对数组存储的一种改进,数组虽然可以存储基本数据类型和引用数据类型,但是在一个数组中,只能存储相同类型的元素,而集合类只能存储引用数据类型,但在一个集合内,可以存储不用类型的元素。同时数组的长度是固定的,但是集合的长度是动态变化的。
集合类框架:
1.collection功能
a.add()addAll();
Student stu1 = new Student("王小明",21);
Student stu2 = new Student("李小乐", 19);
//add()是给集合中添加元素
Collection con = new ArrayList();
con.add(stu1);
con.add(stu2);
Collection con2=new ArrayList();
con2.add(1);
//addAll是将集合2中的元素加在集合1的末尾
con.addAll(con2);
System.out.println(con.toString());
b.clear() remove() removeAll();
//clear()移除某个集合中的全部元素
Collection c = new ArrayList();
c.add(1);
c.add(2);
c.add(3);
c.add(4);
c.clear();
System.out.println(c);//结果是一个空的集合
//remove()是移除某个集合中的某个元素
Collection c = new ArrayList();
c.add(1);
c.add(2);
c.add(3);
c.add(4);
c.remove(1);
System.out.println(c);//结果是[2,3,4]
//removeAll()是移除A集合中的和B集合相同的元素
Collection c = new ArrayList();
c.add(1);
c.add(2);
c.add(3);
c.add(4);
Collection c2=new ArrayList();
c2.add(1);
c2.add(2);
c2.add(3);
c2.add(6);
c2.add(8);
boolean b = c.removeAll(c2);
if(b){
System.out.println("成功");
}else {
System.out.println("失败");
}
System.out.println(c);//[4]
System.out.println(c2);//[1,2,3,6,8]
c.contains()containsAll
d.iterator
e.int size()元素个数 也可以当做长度
f.retainAll()交集功能
g.将集合转化成数组可以用toArray() 数组转化成集合可以asList方法,但是数组转化成集合,不可以进行添加删除操作,因为在定义数组的时候,长度已经是固定的。
- collection集合的遍历,遍历可以利用迭代器进行遍历
Collection<Student> student=new ArrayList<>();
student.add(new Student("王小明",20));
student.add(new Student("白小花",19));
Iterator<Student> iterator = student.iterator();//调用迭代器
while (iterator.hasNext()//hasNext用来判断集合中下一个是否由元素){
Student next = iterator.next();//获得这个元素
System.out.println(next.getName()+"今年"+next.getAge()+"岁");
}
- list集合
List是collection的子类,list拥有collection所拥有的功能外,也有自己新的功能。
//a.add(index,element)在指定索引处添加元素
List<String> str=new ArrayList<String>();
str.add("This");
str.add("is");
str.add("Java!");
str.add(0,"hello");
System.out.println(str.toString());//[hello, This, is, Java!]
//b.remove(index) 移除指定索引处的元素
str.remove(1);
System.out.println(str.toString());//[hello,is,Java!]
//c. get(index) 获取指定索引处的元素
String s = str.get(str.size() - 1);
System.out.println(s);//[Java!]
//d.set(index,element)更换指定索引处的元素,返回被替换的元素
String s2 = str.set(str.size()-1,"iloveit");
System.out.println(s2);//[Java!]
System.out.println(str);//[This, is, iloveit]
- list的遍历功能(3种方法)
// List<String> word = new ArrayList();//只能存引用数据类型 string是一个类
List<Integer> word2 = new ArrayList<>();//int类型不能传,所以传数字用Integer
word2.add(1);
word2.add(2);
word2.add(3);
word2.add(4);
word2.add(5);
word2.add(6);
word2.add(7);
//方式一:for循环(首推)
for(int i=0;i<word2.size();i++){
Integer integer1 = word2.get(i);
System.out.print(integer1+" ");
//1 2 3 4 5 6 7
}
//方式二:用迭代器 Iterator 进行遍历
System.out.println("\t");
Iterator<Integer> iterator = word2.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");//1 2 3 4 5 6 7
}
//方式三:用List本身的遍历方法
System.out.println("\t");
ListIterator<Integer> in = word2.listIterator();
while (in.hasNext()){
System.out.print(in.next()+" ");
//1 2 3 4 5 6 7
}
//hasPrevious()和previous()是ListIterator的特有功能
//hasPrevious()是判断是否存在前一个元素
//previous()是返回列表中的前一个元素
//但是反向遍历需要注意的是,集合正向遍历后应当指向最后一个元素,从而再进行反向遍历
System.out.println("\t");
while(in.hasPrevious()){
System.out.print(in.previous()+" ");
//7 6 5 4 3 2 1
}
}