什么是集合:
- 集合和数组一样,可以保存一组数据,并且提供了操作集合元素的相关方法,使用更加方便(数组速度快,集合方法多)
java集合框架中的相关接口Collection:
- java.util.Collection接口:是所有集合的顶级接口,封装了所有集合所共有的方法,下面有多种实现类(比如arraylist),因此我们可以有更多的数据结构选择。
Object:封装了所有类所共有的方法
Collection:封装了所有集合所共有的方法
Collection接口下面有两种常见的子接口:---------------------------明天详细介绍
- java.util.List:线性表,是可以重复集合,并且有序
- -java.util.Set:不可重复集合,大部分实现类是无序的
Collection接口常用方法:
new的是实现类 最常用的是ArrayList
向上造型 c1只能点出Collection接口的方法
add():向集合中添加一个元素,成功添加则返回true 只要是object类型
size():返回当前集合的元素个数
isEmpty():判断当前集合是否为空集,当且仅当size()为0时返回true
clear():清空集合
contains():判断集合是否包含给定元素
remove():从集合中删除给定元素,成功删除则返回true
addAll():将参数组定集合中的元素添加到当前集合中,添加后当前集合发生改变则返回true
containsAll():判断当前集合中是否包含参数集合中的所有元素
retainAll():取交集
removeAll():删交集
4. 集合的遍历:
5. 增强for循环/新循环:
jdk1.5推出一个新特性:增强型for循环,相同的语法遍历集合和数组
package apiday03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class NewForDemo {
public static void main(String[] args) {
//for循环遍历数组
String[] array={"one","two","three","four","five"};
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
//增强for循环遍历数组 被编译器改为迭代器的方式
//元素类型 变量名 集合或者数组
for(String str:array){
System.out.println(str.toUpperCase());//强转才可以用string方法
}
//迭代器遍历集合
Collection c=new ArrayList();
c.add("one");
c.add("two");
c.add("three");
c.add("four");
c.add("five");
Iterator it=c.iterator();
while(it.hasNext()){
String str=(String)it.next();//object强转为string
System.out.println(str);
}
//增强for循环遍历集合 被编译器改为迭代器的方式
//元素类型 变量名 集合或者数组
for(Object obj:c){
String str=(String)obj;//强转一下
System.out.println(str.toUpperCase());//强转才可以用string方法
}
}
}
6. 泛型:
jdk1.5推出一个新特性
泛型也称为参数化类型,允许我们在使用一个类时,传入某个类型来规定其内部属性,方法参数或返回值类型,使得我们使用时更加方便。
- 泛型在集合运用最广,用来指定集合中元素类型(用迭代器和新循环不用强转string了)
- 若不指定泛型的具体类型,则默认为object
- 若指定泛型的具体类型,则在获取泛型的值时,编译器会补充强转操作
package apiday03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
//泛型
public class GenericDemo {
public static void main(String[] args) {
//point类型
Collection<Point> a =new ArrayList<>();
a.add(new Point(1,2));
a.add(new Point(3,4));
a.add(new Point(5,6));
a.add(new Point(7,8));
Iterator<Point> it=a.iterator();
while(it.hasNext()){
Point p=it.next();
System.out.println(p);
}
for(Point p:a){
System.out.println(p);
}
Collection<String> c=new ArrayList<>();
c.add("one");
c.add("two");
c.add("three");
c.add("four");
c.add("five");
c.add("123");//不符合泛型的实际类型
//迭代器所指定的泛型应该与其遍历的集合泛型一致
Iterator<String> it=c.iterator();
while(it.hasNext()){
String str=it.next();
System.out.println(str);
}
for(String str:c){
System.out.println(str);
}
}
}
7. 集合和数组的转换:
- 集合转换为数组 .toArray()
若参数数组元素个数==集合元素,那就正常转换 若参数数组元素个数<集合元素,那就正常转换(按照集合大小给数组) 若参数数组元素个数>集合元素,那就正常转换,同时结尾给默认值
```java
public class CollectionToArrayDemo {
public static void main(String[] args) {
Collection<String> c = new ArrayList<>();
c.add("one");
c.add("two");
c.add("three");
c.add("four");
c.add("five");
System.out.println(c); //[one, two, three, four, five]
//若参数数组元素个数==集合元素个数,那就正常转换
//若参数数组元素个数<集合元素个数,那就正常转换(按照集合大小给数组)
//若参数数组元素个数>集合元素个数,那就正常转换,同时末尾被默认值
String[] array = c.toArray(new String[6]);
System.out.println(Arrays.toString(array));
}
}
```
- 数组转换为集合 Arrays类的静态方法asList()
ArrayList<>(); asList
```java public class ArrayToCollectionDemo { public static void main(String[] args) { String[] array = {"one","two","three","four","five"}; System.out.println("array:"+Arrays.toString(array)); //asList()方法会返回内部的ArrayList //该ArrayList内部直接引用给定数组array List<String> list = Arrays.asList(array); System.out.println("List: "+list); //对数组操作后,集合也会做相应的改变 array[1] = "six"; System.out.println("array:"+Arrays.toString(array)); System.out.println("List: "+list); //对集合操作后,数组也会做相应的改变 list.set(2,"seven"); //将下标2的元素修改为seven---明天详细讲 System.out.println("array:"+Arrays.toString(array)); System.out.println("List: "+list); //添加元素/删除元素相当于要往数组中加元素/减元素 //而数组是定长的,不会自动扩容/缩容,因此会发生不支持操作的异常 //list.add("!!!!!!"); //运行时发生不支持操作异常 } } ```