day2collections泛型

01.第一章:Collection集合_集合概述
1).Java中提供了大量的“集合类”,它们的功能都是一样的:存储、管理大量的对象。只是不同的集合类,内部采用了“不同的存储形式”–数据结构,采用不同的数据结构,会导致对集合元素的增、删、改、查的效率不同。
2).数据结构:存储数据的方式;
常见的数据结构:
1).数组:ArrayList(线程不安全的,效率高)、Vector(线程安全的,效率低)。
2).链表:LinkedList
3).哈希表:HashSet、HashMap
4).树:TreeSet,TreeMap
5).栈:后进先出
6).队列:先进先出
3).集合体系结构概述:

02.第一章:Collection集合_Collection接口中的常用功能
1).添加的方法:
1).public boolean add(E e) : 把给定的对象添加到当前集合中 。
关于返回值:对于List集合基本上永远返回true。
对于Set集合当存储重复元素时,添加失败,才会返回false。
2).删除的方法:
2).public void clear() :清空集合中所有的元素。
3).public boolean remove(E e) : 把给定的对象在当前集合中删除。
3).查询(获取)的方法:
4).public boolean contains(Object obj) : 判断当前集合中是否包含给定的对象。
5).public boolean isEmpty() : 判断当前集合是否为空。
6).public int size() : 返回集合中元素的个数。
7).public Object[] toArray() : 把集合中的元素,存储到数组中
8).public Iterator iterator():获取一个“迭代器”对象–遍历集合元素。

都是true那怎样才是true呢
add(10) 数据不一样


03.第二章:迭代器_迭代器Iterator接口的使用
1).java.util.Iterator(接口):一个迭代器的接口。
2).常用方法:
1).public boolean hasNext():是否有元素可迭代。是返回:true,否则返回:false。
2).public E next():获取迭代器中的一个元素。
3).示例代码:
public class Demo {
public static void main(String[] args) {
Collection list = new ArrayList<>();

    list.add("张三丰");
    list.add("张无忌");
    list.add("张翠山");
    list.add("周芷若");

    //获取迭代器遍历
    Iterator<String> it = list.iterator();
    /*System.out.println(it.hasNext());//true
    System.out.println(it.next());//张三丰

    System.out.println(it.hasNext());//true0
    System.out.println(it.next());

    System.out.println(it.hasNext());//true
    System.out.println(it.next());

    System.out.println(it.hasNext());//true
    System.out.println(it.next());

    System.out.println(it.hasNext());//false
    System.out.println(it.next());//抛出异常:java.util.NoSuchElementException*/

    while (it.hasNext()) {
        System.out.println(it.next());
    }
}

}

04.第二章:迭代器_迭代器的实现原理

05.第二章:迭代器_使用时的常见错误
1).一次hasNext()判断,两次next()获取:
public class Demo {
public static void main(String[] args) {
Collection list = new ArrayList<>();

    list.add("张三丰");
    list.add("张无忌");
    list.add("张翠山");
    list.add("周芷若");

    //1.获取迭代器
    Iterator<String> it = list.iterator();
    //2.遍历--常见的错误
    while (it.hasNext()) {
        String s = it.next();
        System.out.println(it.next());//第二次next(),错误的,元素跳跃
    }
}

}
2).并发修改异常:
public class Demo {
public static void main(String[] args) {
Collection list = new ArrayList<>();

    list.add("张三丰");
    list.add("张无忌");
    list.add("张翠山");
    list.add("周芷若");

    //1.获取迭代器
    Iterator<String> it = list.iterator();
   //3.第二个常见错误--并发修改异常
    while (it.hasNext()) {
        String s = it.next();
        if(s.equals("张无忌")){

// list.remove(“张无忌”);//当使用迭代器遍历时,不要通过"集合"对象去添加、删除集合元素,否则会引发并发修改异常
// list.add(“呵呵”);
it.remove();
}

        System.out.println(s);
    }

    System.out.println("集合元素:" + list);
}

}
06.第二章:迭代器_增强for
1).一种新的编写for循环的语法:
for(数据类型 变量名 : 数组名/集合名){
}
2).示例:遍历数组:
int[] arr = {1, 2, 3, 4, 5, 6};

for (int n : arr) {//语法糖:假的;编译后:普通for循环
System.out.println(n);
}
3).示例:遍历集合:
ArrayList list = new ArrayList<>();
list.add(“张三丰”);
list.add(“张无忌”);
list.add(“张翠山”);
list.add(“周芷若”);

for (String str : list) {//编译后:迭代器
if (str.equals(“张无忌”)) {
list.add(“呵呵”);//引发并发修改异常,就说明增强for就是迭代器。
}
System.out.println(str);
}
--------------------------------------【了解】---------------------------------
07.第三章:泛型_泛型概述及好处
1).在定义集合时,我们是希望集合中只存储一种类型的引用,这时可以使用泛型:
ArrayList list = new ArrayList<>(); //JDK7以后可以这样写
或者:
ArrayList list = new ArrayList();//JDK7以前必须这样写
2).泛型的好处:可以规定一个集合中只能存储什么一种固定的类型。当不小心存储其它类型时,编译器会编译错误。
ArrayList list = new ArrayList<>();
list.add(“abc”);
list.add(10);//编译错误

08.第三章:泛型_定义和使用含有泛型的类
public class MyArrayList {
public void add(E e) {

}

public E get(){
    return null;
}

}
注意:
1).:表示定义了一个“泛型”,它表示“一种类型”,只有在使用这个类时,才定义这个类型。
2).:语法:可以大写、可以小写、可以一个字母,也可以是多个字母。
09.第三章:泛型_含有泛型的方法
public class MyList {
public void show(E e1,E e2,E e3) {
System.out.println(“show()…”);
}
}
测试类:
public class Demo {
public static void main(String[] args) {
MyList myList = new MyList();

    myList.<Integer>show(10,20,30);

    myList.<String>show("abc", "bbb", "ccc");
}

}
强制一个方法固定接受几个相同类型的时候用

10.第三章:泛型_含有泛型的接口
1).定义方法同“泛型类”一样:
public interface Animal {
public void show(E e);
public E get();
}

11.第三章:泛型_泛型通配符
1).? : 可以表示:具有任何泛型的集合对象

public class Demo {
public static void main(String[] args) {
ArrayList objList = new ArrayList<>();
ArrayList intList = new ArrayList<>();
ArrayList strList = new ArrayList<>();

    /*fun1(objList);//OK的
    fun1(strList);//错误
    fun1(intList);//错误*/
    
    fun2(objList);
    fun2(strList);
    fun2(intList);
    
}
public static void fun1(ArrayList<Object> object) {

}
//需求:定义一个方法,可以接收具有任何泛型的ArrayList对象
public static void fun2(ArrayList<?> object) {

}

}
2).<? extends E>:表示可以接收:E及其E的子类泛型。设定了“上限”:

class Person{}
class Student extends Person{}
class JavaStudent extends Student{}
class PHPStudent extends Student{}

class Teacher extends Person{ }

public class Demo {
public static void main(String[] args) {
ArrayList list1 = new ArrayList<>();

    ArrayList<Student> list2 = new ArrayList<>();
    ArrayList<JavaStudent> list3 = new ArrayList<>();
    ArrayList<PHPStudent> list4 = new ArrayList<>();

    ArrayList<Teacher> list5 = new ArrayList<>();

// fun(list1);//错误
fun(list2);
fun(list3);
fun(list4);
// fun(list5);//错误

}

//需求:定义一个方法,可以接收具有Student及其子类泛型的ArrayList对象
public static void fun(ArrayList<? extends Student> list) {

}

}
3).<? super E>:表示可以接收:E及其E的父类泛型。设定了“下限”:
public class Demo {
public static void main(String[] args) {
ArrayList list1 = new ArrayList<>();

    ArrayList<Student> list2 = new ArrayList<>();
    ArrayList<JavaStudent> list3 = new ArrayList<>();
    ArrayList<PHPStudent> list4 = new ArrayList<>();

    ArrayList<Teacher> list5 = new ArrayList<>();

    fun(list1);
    fun(list2);

// fun(list3);//错误
// fun(list4);//错误
// fun(list5);//错误

}

//需求:定义一个方法,可以接收具有Student及”父类“类泛型的ArrayList对象
public static void fun(ArrayList<? super Student> list) {

}

}

12.第四章:综合案例_模拟斗地主洗牌发牌【熟练10分钟】
1).封装一副牌,存储到一个ArrayList中;
2).洗牌–打乱集合中元素的顺序;
Collections.shuffle(List<?> list);
3).发牌–没人一个集合,存储17个字符串,底牌集合,存3张底牌;
4).看牌–打印每个人的集合

package cn.itheima.demo11_模拟斗地主洗牌发牌程序;

import java.util.ArrayList;
import java.util.Collections;

public class Demo {
public static void main(String[] args) {
// 1).封装一副牌,存储到一个ArrayList中;
//1.准备一个ArrayList集合
ArrayList pokerList = new ArrayList<>();
pokerList.add(“大王”);
pokerList.add(“小王”);
String[] colors = {“♥”, “♠”, “♦”, “♣”};
String[] numbers = {“2”, “A”, “K”, “Q”, “J”, “10”, “9”, “8”, “7”, “6”,
“5”, “4”, “3”};

    for (String n : numbers) {//2
        for (String c : colors) {//"♥", "♠", "♦", "♣"
           pokerList.add(c + n);
        }
    }

// 2).洗牌–打乱集合中元素的顺序;
Collections.shuffle(pokerList);

// 3).发牌–每人一个集合,存储17个字符串,底牌集合,存3张底牌;
ArrayList list1 = new ArrayList<>();
ArrayList list2 = new ArrayList<>();
ArrayList list3 = new ArrayList<>();
ArrayList dipaiList = new ArrayList<>();
for (int i = 0; i < pokerList.size(); i++) {
//先判断底牌
if (i >= 51) {
dipaiList.add(pokerList.get(i));
}else{
if (i % 3 == 0) {
list1.add(pokerList.get(i));
} else if (i % 3 == 1) {
list2.add(pokerList.get(i));
} else if (i % 3 == 2) {
list3.add(pokerList.get(i));
}
}
}
// 4).看牌–打印每个人的集合
System.out.println(“特朗普:” + list1);
System.out.println(“金三胖:” + list2);
System.out.println(“普京:” + list3);
System.out.println(“底牌:” + dipaiList);

}

}

  1. 下面哪个功能不是Collection集合的? ()
    A: add(E e)
    B: remove(Object o)
    C: length()
    D: isEmpty()
  2. 下面程序的输出结果是? ()
    Collection coll = new ArrayList();
    coll.add(“hello”);
    coll.add(“world”);
    coll.add(“java”);
    System.out.println(coll.remove(“java”));
    A: java
    B: null
    C: true
    D: 编译报错
  3. 观察下列代码
    public class cs {
    public static void main(String[] args) {
    Collection coll = new ArrayList();
    coll.add(“天安门”);
    coll.add(“兵马俑”);
    Iterator it = coll.iterator();
    System.out.print(it.next());
    System.out.print(it.next());
    System.out.print(it.next());
    }
    }
    请问,下面哪种说法是正确的? ()
    A: 编译报错
    B: 天安门兵马俑Exception in thread “main” java.util.NoSuchElementException
    C: 在控制台输出:天安门兵马俑
    D: 在控制台输出:天安门兵马俑null
  4. 观察下列代码
    public static void main(String[] args) {
    Collection coll = new ArrayList();
    coll.add(“水立方”);
    coll.add(“东方明珠”);
    coll.add(“大雁塔”);
    for (String s : coll) {
    System.out.print(s+", ");
    }
    }

最终结果是? ()
A: 编译报错
B: 运行报错
C: 在控制台输出:水立方, 东方明珠, 大雁塔,
D: 以上都不对
5. 下面代码的输出结果是? ()
public static void main(String[] args) {
Collection arr=new ArrayList();
arr.add(“张无忌”);
arr.add(“张翠山”);
arr.add(“赵敏”);
arr.add(“杨不悔”);
for (String str : arr) {
if(str.equals(“张翠山”)){
arr.remove(str);
}
}
for (String name : arr) {
System.out.print(name+" ");
}
}
A: 张无忌 赵敏 杨不悔
B: 张无忌 张翠山 赵敏 杨不悔
C: 运行出错
D: 张翠山

  1. 观察下列代码
    Collection coll = new ArrayList<>();
    coll.add(“s”);
    coll.add(“ddd”);
    coll.add(“true”);
    System.out.println(coll);

运行结果是? ()
A: 编译报错
B: 运行报错
C: [s, ddd, true]
D: [true]
7. 下面代码运行的结果是?()
Collection arr=new ArrayList();
arr.add(“1”);
arr.add(“java”);
arr.add(1);
arr.add(true);
System.out.println(arr);
A: 集合的地址值
B: 1java1true
C: 编译出错
D: 1java
8. 出现泛型:<? extends 类 >,下面哪个说法是正确的?()
A: 只能接收该类型及其子类
B: 只能接收该类型
C: 只能接收该类型的子类
D: 只能接收该类型及其父类型
9. 一个类声明上出现泛型:<? super 类 >,下面哪个说法是正确的?()
A: 该泛型只能接收该类型及其子类
B: 该泛型只能接收该类型
C: 该泛型只能接收该类型的父类
D: 该泛型只能接收该类型及其父类型

发布了4 篇原创文章 · 获赞 0 · 访问量 54

猜你喜欢

转载自blog.csdn.net/lujunskli/article/details/105485442
今日推荐