Collection、Iterator(迭代器)、增强for循环、Generic(泛型)

目录

1、Collection(所有单列集合的父接口,定义的是所有单列集合(List和Set)中共性的方法)

(1)使用add()方法向集合中添加元素

(2)使用remove()方法从集合中删除指定的元素(返回boolean类型数据)

(3)使用contains()方法判断当前集合中是否包含给定元素(返回boolean类型数据)

(4)使用isEmpty()方法判断当前集合是否为空(返回boolean类型数据)

(5)使用size()方法返回集合中元素的个数(返回int类型数据)

(6)使用toArray()方法把集合中的元素存储到数组中

(7)使用clear()方法清空集合中所有的元素(并非删除集合,集合还存在)

2、Iterator(迭代器)

(1)使用while循环遍历

(2)使用for循环遍历 

3、增强for循环(用来遍历集合和数组)(快捷键:数组名/集合名.for -> 回车)

(1)使用增强for循环来遍历集合 

(2)使用增强for循环来遍历数组 

4、Generic(泛型)

(1)普通泛型示例

a)使用泛型

b)不使用泛型

(2)泛型类

a)不使用泛型类(只能设置指定类型的数据,很不方便)

b)使用泛型类(可以设置多种类型的数据)

(3)泛型方法

a)不使用含有泛型的方法

b)使用含有泛型的方法

(4)泛型接口

(5)泛型的通配符  ?


1、Collection(所有单列集合的父接口,定义的是所有单列集合(List和Set)中共性的方法)

(1)使用add()方法向集合中添加元素

//首先创建集合对象,可以使用多态
Collection<String> coll = new ArrayList<>();
System.out.println(coll);// [] ,重写了toString方法
//向集合中添加元素
coll.add("张三");
coll.add("李四");
coll.add("迪丽热巴");
coll.add("古力娜扎");
coll.add("马尔扎哈");
System.out.println(coll);

打印结果:
[]
[张三, 李四, 迪丽热巴, 古力娜扎, 马尔扎哈]

(2)使用remove()方法从集合中删除指定的元素(返回boolean类型数据)

【还使用(1)中的coll集合】
//删除指定的元素
boolean rem = coll.remove("张三");//删除“张三”
if (rem == true) {
    System.out.println("删除成功,剩下的集合为:" + coll);
} else {
    System.out.println("删除失败,剩下的集合为:" + coll);
}
boolean rem2 = coll.remove("王五");//删除“王五”
if (rem2 == true) {
    System.out.println("删除成功,剩下的集合为:" + coll);
} else {
    System.out.println("删除失败,剩下的集合为:" + coll);
}

打印结果:
删除成功,剩下的集合为:[李四, 迪丽热巴, 古力娜扎, 马尔扎哈]
删除失败,剩下的集合为:[李四, 迪丽热巴, 古力娜扎, 马尔扎哈]

(3)使用contains()方法判断当前集合中是否包含给定元素(返回boolean类型数据)

【还使用(1)中的coll集合】
//判断当前集合中是否包含给定元素
boolean co = coll.contains("迪丽热巴");
if (co == true){
    System.out.println("集合中包含此元素");
}else {
    System.out.println("集合中不包含此元素");
}
boolean co1 = coll.contains("张三");
if (co1 == true){
    System.out.println("集合中包含此元素");
}else {
    System.out.println("集合中不包含此元素");
}

打印结果:
集合中包含此元素
集合中不包含此元素

(4)使用isEmpty()方法判断当前集合是否为空(返回boolean类型数据)

【还使用(1)中的coll集合】
//判断当前集合是否为空
boolean empty = coll.isEmpty();
System.out.println("当前集合是否为空?" + empty);

打印结果:
当前集合是否为空?false

(5)使用size()方法返回集合中元素的个数(返回int类型数据)

【还使用(1)中的coll集合】
//返回集合中元素的个数
int size = coll.size();
System.out.println("当前集合有" + size + "个元素");

打印结果:
当前集合有5个元素

(6)使用toArray()方法把集合中的元素存储到数组中

【还使用(1)中的coll集合】
//把集合中的元素存储到数组中
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {//遍历数组(快捷键:arr.fori -> 回车)
    System.out.println(arr[i]);
}

打印结果:
张三
李四
迪丽热巴
古力娜扎
马尔扎哈

(7)使用clear()方法清空集合中所有的元素(并非删除集合,集合还存在)

【还使用(1)中的coll集合】
//清空集合中所有的元素,但是不删除集合,集合还存在
coll.clear();
System.out.println(coll);//[]
boolean empty1 = coll.isEmpty();//判断是否为空
System.out.println("当前集合是否为空?" + empty1);

打印结果:
[]
当前集合是否为空?true

2、Iterator(迭代器)

Iterator迭代器,是一个接口,我们无法直接使用,需要使用Iterator接口的实现类对象。 Collection接口中有一个方法,叫iterator(),这个方法返回的就是迭代器的实现类对象

(1)使用while循环遍历

//创建一个集合对象
Collection<String> coll = new ArrayList<>();
//往集合中添加元素
coll.add("库里");
coll.add("汤普森");
coll.add("欧文");
coll.add("詹姆斯");
coll.add("杜兰特");

//获取迭代器的实现类对象
Iterator<String> iterator1 = coll.iterator();//多态写法
//循环遍历,输出打印【while循环】
while (iterator1.hasNext()) {//也可以写while (iterator1.hasNext()==true)
    System.out.println(iterator1.next());
}

打印结果:
库里
汤普森
欧文
詹姆斯
杜兰特

(2)使用for循环遍历 

//创建一个集合对象
Collection<String> coll = new ArrayList<>();
//往集合中添加元素
coll.add("库里");
coll.add("汤普森");
coll.add("欧文");
coll.add("詹姆斯");
coll.add("杜兰特");

//获取迭代器的实现类对象
Iterator<String> iterator2 = coll.iterator();//多态写法
//循环遍历,输出打印【for循环】
for (int i = 0; i < coll.size(); i++) {
    if (iterator2.hasNext()) {
        System.out.println(iterator2.next());
    }
}

打印结果:
库里
汤普森
欧文
詹姆斯
杜兰特

3、增强for循环(用来遍历集合和数组)(快捷键:数组名/集合名.for -> 回车)

(1)使用增强for循环来遍历集合 

//创建一个ArrayList集合
ArrayList<String> list = new ArrayList<>();
list.add("迪丽热巴");
list.add("古力娜扎");
list.add("马尔扎哈");
//使用增强for循环遍历集合
for (String s : list) {//快捷键:list.for -> 回车
    System.out.println(s);
}

打印结果:
迪丽热巴
古力娜扎
马尔扎哈

(2)使用增强for循环来遍历数组 

//创建一个int类型的数组
int[] arr = {1, 2, 3, 4, 5};
//使用增强for循环遍历数组
for (int i : arr) {//快捷键:arr.for -> 回车
    System.out.println(i);
}

打印结果:
1
2
3
4
5

4、Generic(泛型)

(1)普通泛型示例

a)使用泛型

好处:避免了类型转换的麻烦,存储的是什么类型,取出的就是什么类型。另外,把运行期异常提升到了编译期(只要程序出现异常,就直接报错,不会等到运行之后才发现异常)

弊端:泛型是什么类型,就只能存储什么类型的数据(这个弊端可以忽略)

ArrayList<String> arr = new ArrayList<>();//使用泛型,只能向集合中添加String类型的数据
arr.add("迪丽热巴");
arr.add("古力娜扎");
arr.add("马尔扎哈");
//arr.add(123);//泛型是String类型,就只能存储String类型的数据,因此会报错

//使用迭代器循环遍历打印
Iterator<String> iterator1 = arr.iterator();
while (iterator1.hasNext()){
    System.out.println(iterator1.next());
}

打印结果:
迪丽热巴
古力娜扎
马尔扎哈

b)不使用泛型

好处:集合不使用泛型,默认的类型就是Object类型,可以存储任意类型的数据

弊端:集合不安全,会引发异常。因为不使用泛型,默认Object类型,则一定是使用多态写法(Object obj1 = "zhangsan"、Object obj2 = 123)。但是如果想获取每个字符串的长度时,就要用到String类中特有的length()方法,但是多态的弊端就是不能使用子类特有的方法,因此就需要向下转型。obj1 = "zhangsan"可以向下转型为String类型,就可以调用length()方法获取其长度,但是要想获取obj1 = 123的长度,因为int类型的数据不能转型为String类型,因此会报错。这也就是不安全,会引发异常的原因。

ArrayList list = new ArrayList();//不使用泛型,就是默认Object类型,可以向集合中添加任何类型的数据
list.add("迪丽热巴");
list.add(123);
list.add('中');
list.add(3.14);
list.add(true);

//使用增强for循环遍历
for (Object s : list) {
    System.out.println(s);
}

打印结果:
迪丽热巴
123
中
3.14
true

(2)泛型类

a)不使用泛型类(只能设置指定类型的数据,很不方便)

1️⃣创建一个Student类,用来创建对象使用,指定具体数据类型为String类型(不使用泛型)

package com.itheima.demo03Generic;

public class Student {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

2️⃣创建一个DemoStudent类,使用Student类来创建对象,并传递参数

package com.itheima.demo03Generic;

public class DemoStudent {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("张三");//只能设置String类型的数据
        System.out.println(student.getName());
//        student.setName(123);//错误写法,因为指定了泛型为String类型,就不能设置Integer类型的数据
    }
}

打印结果:
张三

b)使用泛型类(可以设置多种类型的数据)

泛型是一个未知的数据类型,当我们不确定使用什么数据类型时,可以使用泛型,泛型可以接收任意的数据类型

1️⃣创建一个GenericClass类,用来创建对象使用,不指定具体数据类型(使用泛型)

package com.itheima.demo03Generic;

public class GenericClass<E> {
    //自定义泛型
    private E name;
    public E getName() {
        return name;
    }
    public void setName(E name) {
        this.name = name;
    }
}

2️⃣创建一个DemoGenericClass类,使用GenericClass类来创建对象,并传递参数

package com.itheima.demo03Generic;

public class DemoGenericClass {
    public static void main(String[] args) {
        //不指定泛型,默认为Object类型,什么类型的数据都可以赋值
        GenericClass gc1 = new GenericClass();
        gc1.setName("张三");
        System.out.println(gc1.getName());
        gc1.setName(123);
        System.out.println(gc1.getName());
        System.out.println("===========");

        //使用Integer泛型
        GenericClass<Integer> gc2 = new GenericClass<>();
        gc2.setName(123);
        System.out.println(gc2.getName());
//        gc2.setName("张三");//错误写法,因为指定了泛型为Integer类型,就不能设置String类型的数据
        System.out.println("==========");

        //使用String泛型
        GenericClass<String> gc3 = new GenericClass<>();
        gc3.setName("张三");
        System.out.println(gc3.getName());
//        gc3.setName(123);//错误写法,因为指定了泛型为String类型,就不能设置Integer类型的数据
    }
}

打印结果:
张三
123
===========
123
==========
张三

(3)泛型方法

a)不使用含有泛型的方法

1️⃣创建一个GenericMethod类,用来创建对象和调用方法使用(不使用泛型,指定类型)

package com.itheima.demo03Generic;

public class GenericMethod {
    //定义一个不含泛型的普通方法
    public void method1(int i){//只能传递int类型的参数
        System.out.println(i);
    }

    //定义一个不含泛型的静态方法
    public static void method2(String s){//只能传递String类型的参数
        System.out.println(s);
    }
}

2️⃣ 创建一个DemoGenericMethod类,用来创建GenericMethod对象和调用其中的方法

package com.itheima.demo03Generic;

public class DemoGenericMethod {
    public static void main(String[] args) {
        //创建GenericMethod对象
        GenericMethod gm = new GenericMethod();
        gm.method1(123);//调用method1方法,并且只能传递int类型的参数
        System.out.println("===========");

        gm.method2("静态方法,不建议创建对象使用!");
        //静态方法,通过类名.方法名(参数)  调用
        GenericMethod.method2("张三");//调用method2方法,并且只能传递String类型的参数
    }
}

打印结果:
123
静态方法,不建议创建对象使用!
张三

b)使用含有泛型的方法

1️⃣创建一个GenericMethod类,用来创建对象和调用方法使用(使用泛型)

package com.itheima.demo03Generic;

public class GenericMethod {
    //定义一个含有泛型的普通方法
    public <M> void method1(M m){
        System.out.println(m);
    }

    //定义一个含有泛型的静态方法
    public static <S> void method2(S s){
        System.out.println(s);
    }
}

2️⃣ 创建一个DemoGenericMethod类,用来创建GenericMethod对象和调用其中的方法

package com.itheima.demo03Generic;

public class DemoGenericMethod {
    public static void main(String[] args) {
        //创建GenericMethod对象
        GenericMethod gm = new GenericMethod();
        //调用含有泛型的方法method1。传递什么类型,泛型就是什么类型
        gm.method1("张三");
        gm.method1(123);
        gm.method1(true);
        gm.method1(3.14);
        gm.method1('于');
        System.out.println("===========");

        gm.method2("静态方法,不建议创建对象使用!");
        //静态方法,通过类名.方法名(参数)  调用
        GenericMethod.method2("张三");
        GenericMethod.method2(123);
        GenericMethod.method2(true);
        GenericMethod.method2(3.14);
        GenericMethod.method2('于');
    }
}

打印结果:
张三
123
true
3.14
于
===========
静态方法,不建议创建对象使用!
张三
123
true
3.14
于

(4)泛型接口

1️⃣创建一个含有泛型的接口,里面写一个抽象方法

package com.itheima.demo03Generic;
//定义含有泛型的接口
public interface GenericInterface<I> {
    public abstract void method(I i);
}

2️⃣定义一个接口的实现类GenericInterfaceImpl1,用于实现上面的接口(这个接口实现类指定接口的泛型)

package com.itheima.demo03Generic;
//指定接口的泛型为String
public class GenericInterfaceImpl1 implements GenericInterface<String> {
    @Override
    public void method(String s) {
        System.out.println(s);
    }
}

3️⃣定义一个接口的实现类GenericInterfaceimpl2,用于实现上面的接口(这个接口实现类不指定接口的泛型)

package com.itheima.demo03Generic;
//自定义泛型(不指定接口的泛型)
public class GenericInterfaceImpl2<A> implements GenericInterface<A> {
    @Override
    public void method(A a) {
        System.out.println(a);
    }
}

4️⃣创建一个DemoGenericInterface类,用来测试含有泛型接口

package com.itheima.demo03Generic;
//测试含有泛型的接口
public class Demo04GenericInterface {
    public static void main(String[] args) {
        GenericInterfaceImpl1 impl1 = new GenericInterfaceImpl1();
        impl1.method("迪丽热巴");
//        impl1.method(123);//错误写法,因为指定了泛型为String类型,就不能设置Integer类型的数据

        GenericInterfaceImpl2<String> impl2 = new GenericInterfaceImpl2<>();//创建String类型的对象
        impl2.method("迪丽热巴");//传递String类型的参数

        GenericInterfaceImpl2<Integer> impl3 = new GenericInterfaceImpl2<>();//创建int类型的对象
        impl3.method(123);//传递int类型的参数

        GenericInterfaceImpl2<Double> impl4 = new GenericInterfaceImpl2<>();//创建Double类型的对象
        impl4.method(3.14);//传递double类型的参数
    }
}

打印结果:
迪丽热巴
迪丽热巴
123
3.14

(5)泛型的通配符  ?

不知道使用什么类型来接收数据的时候,此时可以使用 ? 来接收

package com.itheima.demo03Generic;

import java.util.ArrayList;
import java.util.Iterator;
//泛型的通配符:? 代表任意的数据类型
//不能创建对象使用,只能作为方法的参数使用
public class Demo05Generic {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        list1.add("迪丽热巴");
        list1.add("古力娜扎");
        list1.add("马尔扎哈");

        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(123);
        list2.add(456);
        list2.add(789);

        printArray(list1);
        printArray(list2);
    }

    //定义一个方法,能遍历所有类型的ArrayList集合
    //当不知道ArrayList集合使用什么数据类型,可以使用泛型的通配符?来接收数据类型
    public static void printArray(ArrayList<?> list){
        //使用迭代器遍历集合
        Iterator<?> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

打印结果;
迪丽热巴
古力娜扎
马尔扎哈
123
456
789

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值