• 增强for循环,,,泛型


    第1章     增强for循环

    增强for循环是JDK1.5以后出来的一个高级for循环,专门用来遍历数组和集合的。

    它的内部原理其实是个Iterator迭代器,所以在遍历的过程中,不能对集合中的元素进行增删操作

    格式:

    for(元素的数据类型 变量 : Collection集合or数组){
    }

    它用于遍历Collection和数组。通常只进行遍历元素,不要在遍历的过程中对集合元素进行增删操作。

    public class Demo04 {
    public static void main(String[] args) {
        Collection<Person> arr=new ArrayList<Person>();
        arr.add(new Person("小懒猫",18));
        arr.add(new Person("小紫毛",20));
        arr.add(new Person("小黑毛",20));
        for(Person i:arr){//增强for遍历
            System.out.println(i);
        }
        Iterator<Person> it=arr.iterator();
        while(it.hasNext()){//while迭代遍历
            System.out.println(it.next());
        }
        System.out.println();
        //遍历Person数组
        Person[] brr={new Person("小白猫",18),
                new Person("小黑猫",20),
                new Person("小绿帽",21)};
        for(Person i:brr){
            System.out.println(i);
        }
        
    }
    }

    增强for循环和老式的for循环有什么区别?

    注意:新for循环必须有被遍历的目标。目标只能是Collection或者是数组。

    建议:遍历数组时,如果仅为遍历,可以使用增强for如果要对数组的元素进行 操作,使用老式for循环可以通过角标操作。

         泛型

    1.1.1    含有泛型的类

    定义格式:修饰符 class 类名<代表泛型的变量> { }

    例如,API中的ArrayList集合:

    class ArrayList<E>{
    public boolean add(E e){ }
        public E get(int index){ }
    }

    使用格式:创建对象时,确定泛型的类型

    例如,ArrayList<String> list = new ArrayList<String>();

    此时,变量E的值就是String类型

    class ArrayList<String>{
    public boolean add(String e){ }
        public String get(int index){ }
    }

    1.1.1    含有泛型的接口

    定义格式:修饰符 interface接口名<代表泛型的变量> { }

    例如,API中的Iterator迭代器接口

    public interface Iterator<E> {
        public abstract E next();
    }

    使用格式:

    1、定义类时确定泛型的类型

    public final class Scanner implements Iterator<String> {
    public String next(){
     }
    }

    2、始终不确定泛型的类型,直到创建对象时,确定泛型的类型

    例如

    ArrayList<String> list = new ArrayList<String>();

    Iterator<String> it = list.iterator();

    public interface Iterator<String> {
        public abstract String next();
    }

    1.1      使用泛型的好处

    将运行时期的ClassCastException,转移到了编译时期变成了编译失败。

    避免了类型强转的麻烦。

    public class GenericDemo {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("abc");
            list.add("oracle");
            //list.add(5);//当集合明确类型后,存放类型不一致就会编译报错
            //集合已经明确具体存放的元素类型,那么在使用迭代器的时候,迭代器也同样会知道具体遍历元素类型
            Iterator<String> it = list.iterator();
            while(it.hasNext()){
        String str = it.next();
    //当使用Iterator<String>控制元素类型后,就不需要强转了。获取到的元素直接就是String类型
                System.out.println(str.length());
            }
        }
    }

          泛型通配符

    当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示。

    但是一旦使用泛型的通配符后,只能使用Object类中的共性方法,集合中元素自身方法无法使用。

          泛型限定

    限定泛型的上限:

    格式:? extends E

    ? 代表接收E类型或者E的子类型的元素

     例如,泛型限定为:? extends Person

     则 ? 代表接收Person类型或者Person子类型的元素

    限定泛型的下限:

    格式:? super E

    ? 代表接收E类型或者E的父类型的元素

     例如,泛型限定为:? super Student

     则 ? 代表接收Student类型或者Student父类型的元素

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    
    import com.oracle.demo01.Person;
    
    public class Demo01 {
    public static void main(String[] args) {
        Student s1=new Student("小白",8);
        Student s2=new Student("小黑",8);
        Worker s3=new Worker("小子",22);
        Worker s4=new Worker("小兰",20);
        Collection<Student> col=new ArrayList<Student>();
        col.add(s1);
        col.add(s2);
        Collection<Worker> col2=new ArrayList<Worker>();
        col2.add(s3);
        col2.add(s4);
        Collection<String> col3=new ArrayList<String>();
        col3.add("111");
        col3.add("qqq");
        get(col);
        get(col2);
        //get(col3);
    }
    //泛型通配符?
    //? extends Person限定泛型的上限
    //? super Person限定泛型的下限
    public static void get(Collection<? extends Person> c){//E用在定义类或接口上,不能使用
    //学生和工人的共性类型Person。那么,泛型的限定可以这样书写:
    //? extends Person : 接收Person类型或者Person的子类型。
    
        Iterator<?> it=c.iterator();
        while(it.hasNext()){
            //System.out.println(it.next());遍历
            Object obj=it.next();
            //向下转型
            Person p=(Person)obj;
            System.out.println(p.getName());//调子类独有的方法
        }
    }
    }
    import com.oracle.demo01.Person;
    
    public class Student extends Person {
    
        public Student() {
            super();
        }
    
        public Student(String name, int age) {
            super(name, age);
        }
    
    }
    import com.oracle.demo01.Person;
    
    public class Worker extends Person {
    
        public Worker() {
            super();
        }
    
        public Worker(String name, int age) {
            super(name, age);
        }
    
    }
  • 相关阅读:
    zoj 2876 Phone List
    zoj 2829 Beautiful Number
    zoj 2723 Semi-Prime(set)
    zoj 2835 Magic Square(set)
    zoj 2818 Root of the Problem
    zoj 2744 Palindromes
    zoj 1109 Language of FatMouse(map)
    zoj 2724 Windows Message Queue
    zoj 2722 Head-to-Head Match(两两比赛)
    zoj 2727 List the Books
  • 原文地址:https://www.cnblogs.com/111wdh/p/13298168.html
Copyright © 2020-2023  润新知