General description of the collection Collection

As the collection container interface is the highest hierarchical level, which are in the following set of parent-child relationships and, as is the abstract methods inherited ArrayList, on the abstract list is interface inheritance relationship between them as in FIG.

Which, list is an ordered interfaces, set a disorderly interface.

As with all the same set as the interface of the top layer also has a collection mode to create ③ ① ② set traversal methods.

Mode 1: Collection <element type> variable name = new new the ArrayList <element type> (); 
embodiment 2: Collection variable name  = new new the ArrayList ();

Use case to describe. ① create objects

com.oracle.demo Package; 

Import of java.util.ArrayList; 
Import java.util.Collection; 
Import the java.util.Iterator; 
// set of interfaces to the 
public  class Demo03 {
     public  static  void main (String [] args) {
         // upward transition parent class object variable name = new subclass object (); 
        Collection <String> COL = new new the ArrayList <String> (); 
        col.add ( " Hello " ); 
        col.add ( " Ao prop " ); 
        COL .add ( " Dragon King " ); 
        col.add ( " Li Jing ");

ps: to create a collection of all the ways that are carried out in accordance with polymorphic. The meaning here is a string type of a collection point interfaces new ArrayList, i.e. a set of type string ArrayList.

② common method

According java object-oriented thinking, we know that ArrayList implements list is a collection of interfaces, but also inherited the collection list interface. So, collection is also common methods. Such as:

public  class jihe {
     public  static  void main (String [] args) {
         // because it is an interface, new objects can not only be a new polymorphic subclass object 
        Collection <Integer> = COL new the ArrayList <Integer> ();
         // add elements the Add 
        col.add ( 123 ); 
        col.add ( 456 ); 
        col.add ( 789 );
         // remove elements 
        col.remove ( 456 );
         // empty set
     //     col.clear ();
         / / downcast because the set list and incomplete interfaces are ordered, so the index is not public, it needs to take a unique method of the list
         if(COL the instanceof the ArrayList) { 
        the ArrayList <Integer> List = (the ArrayList <Integer> ) COL;
         // reasons not convenient to get method missing collection interface 
        for ( int I = 0 ; I <list.size (); I ++ ) { 
            the System. OUT .println (List. GET (I)); 
            } 
        } 
where the collection may be downward transition, such as
collection col = new Arraylist (); // declaration because if not, then the type of the object type to directly enhance

  coll.add("abc");

  coll.add("aabbcc");

  coll.add("cat");

Iterator it = col.iterator (); // There are two ways downward transition, a direct claim type, Iterator <string> it = col.iterator ();

while(it.hasnext){

  // second: polymorphic downcast.

String str = (String) it.next();

}
         // determines whether the collection contains an element 
        Boolean In Flag = col.contains ( 999 ); 
        . The System OUT .println (In Flag);
         // to set into an array, object can be transferred directly inteager, integer not directly transferred object, no inheritance array 
        Object [] = ARR col.toArray ();
         for ( int I = 0 ; I <arr.length; I ++ ) { 
            the System. OUT .println (ARR [I]); 
            } 
        }

③ traversal.

From the collection method used, we find that he is no way to extract elements can be used. This is the collection of characteristics, there are two methods.

1. iterator Iterator

An Iterator is an abstract interface, which is for collection may be achieved through the array interface, which inherits the methods, such as

The characteristics of the method of rewriting the rewritable method Iterator.

It is a flow diagram

也就是说,hasnext是判断集合中是否有元素,next是执行一次走一次,直到走到集合里面没有元素为止,划重点,hasnext会有一个初始值如上图的程序员,所以我们遍历的时候最好用数值接一下,以防next走两遍出现空值异常。

它的书写流程如下:

public static void main(String[] args) {
        //person类的collection接口
        Collection<Person> col =new ArrayList<Person>();
        col.add(new Person("司南",22));
        col.add(new Person("周戎",25));
        col.add(new Person("楚慈",27));
        //获取迭代器 这里需要注意的是迭代器也是采用的多态关系
        Iterator<Person> it=col.iterator();
        //
        while(it.hasNext()){
            //引用数据类型返回的是字符串
            System.out.println(it.next());
        }
    }

2.增强for

与普通for不同,它只适应于collection的集合和数组之中。它不能增删修改元素。数组可以用普通for的下标进行操作。

创建对象:可以基本数据类型也可以自定义数据类型

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

}

public static void main(String[] args) {
        //容器
        Collection<Person>col1=new ArrayList();
        col1.add(new Person("敖丙",18));
        col1.add(new Person("哪吒",18));
        col1.add(new Person("李靖",30));
        //增强for循环
        for(Person p:col1){
            System.out.println(p);
        }

泛型。泛型的作用是声明集合整个是什么类型,用<>括起来。总共有三种应用场景。

  第一种:定义泛型的类。

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

第二种:定义泛型的接口

 

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

<E>e表示的是万能泛型,也就是传入什么泛型都可以,传入String就是字符串类型

泛型的好处:

 

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

泛型通配符

因为泛型是为了定义类型,也就是说最开始就直接限定死,但是如果我们想打印操作别的数据类型怎么办呢?

当然是用<?>

 

package com.oracle.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo07 {
    public static void main(String[] args) {
        Collection<Person> col=new ArrayList<Person>();
        col.add(new Person("司南",18));
        col.add(new Person("二少",19));
        Collection<Student> col2=new ArrayList<Student>();
        col2.add(new Student("军爷","啦啦"));
        col2.add(new Student("秀秀","lalall"));
        //调用同一个方法,编辑集合
        get(col);
        get(col2);    
    }
    //给get方法传参,传递一个collection不知道啥类的变量,这里意味上面穿什么类型的参数都可以。
    public static void get(Collection<?> col){
        //获取迭代器
        Iterator<?> it=col.iterator();
        //判断下个是否有元素
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }

 

 

 

但是问题接踵而至,如果我们不是相对集合进行全部的类型操作,只想操作其中的一部分,就要用到集合限定,集合的上限和下限。

上限比较好理解,也就是上限 <? extends 集合类型>所有及以上,如<? extends object>所有的object

下限 <? super 集合类型>也就是最少是当前类型及其以上<? extends string>最少是string,也可以是object

首先定义两个类emp顾客类

 

//顾客类
public class Emp {
    public void work(){
        System.out.println("员工工作");
    }

}
//服务员类
public class Waiter extends Emp {
    public void work(){
        //都有一个默认的无参构造
        System.out.println("服务员工作");
    }
//厨师类
public class Cook extends Emp{
    public void work(){
        System.out.println("厨师炒菜");
    }

 

 

 

package com.oracle.demo3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Collection <Cook>col1=new ArrayList<Cook>();
        col1.add(new Cook());
        Collection <Waiter>col2=new ArrayList<Waiter>();
        col2.add(new Waiter());
        //掉一个方法遍历集合
        get(col1);
        get(col2);
        Collection <Scanner>col3=new ArrayList<Scanner>();
        //get(col3);Scanner不是emp的子类,所有不对
    }
    //定义一个方法
    //泛型的上限
    //泛型的下限
    //<? super Emp>,确定最少是emp,其他的可以是父类
    public static void get(Collection<? extends Emp> col){
        Iterator<?> id=col.iterator();
        while(id.hasNext()){
            System.out.println(id.next());
        }
    }//得出厨师炒菜,服务员服务
}

 

Guess you like

Origin www.cnblogs.com/a199706/p/11361374.html