Object类是所有引用数据类型的父类它提供一些方法给子类使用或扩展一、Collection集合集合集合是java中提供的一种容器可以用来存储多个数据。数组的长度是固定的。集合的长度是可变的。数组中存储的是同一类型的元素可以存储基本数据类型值。集合存储的都是对象。而且对象的类型可以不一致。Collection是所有单列集合的父接口因此在Collection中定义了单列集合(List和Set)通用的一些方法这些方法可用于操作所有的单列集合。方法如下public boolean add(E e) 把给定的对象添加到当前集合中 。public void clear():清空集合中所有的元素。public boolean remove(E e): 把给定的对象在当前集合中删除。public boolean contains(E e): 判断当前集合中是否包含给定的对象。public boolean isEmpty(): 判断当前集合是否为空。public int size(): 返回集合中元素的个数。public Object[] toArray(): 把集合中的元素存储到数组中。方法演示import java.util.ArrayList; import java.util.Collection; public class Demo1Collection { public static void main(String[] args) { // 创建集合对象 // 使用多态形式 CollectionString coll new ArrayListString(); // 使用方法 // 添加功能 boolean add(String s) coll.add(小李广); coll.add(扫地僧); coll.add(石破天); System.out.println(coll); // boolean contains(E e) 判断o是否在集合中存在 System.out.println(判断 扫地僧 是否在集合中coll.contains(扫地僧)); //boolean remove(E e) 删除在集合中的o元素 System.out.println(删除石破天coll.remove(石破天)); System.out.println(操作之后集合中元素:coll); // size() 集合中有几个元素 System.out.println(集合中有coll.size()个元素); // Object[] toArray()转换成一个Object数组 Object[] objects coll.toArray(); // 遍历数组 for (int i 0; i objects.length; i) { System.out.println(objects[i]); } // void clear() 清空集合 coll.clear(); System.out.println(集合中内容为coll); // boolean isEmpty() 判断是否为空 System.out.println(coll.isEmpty()); } }二、Iterator迭代器2.1IteratorIterator接口也是Java集合中的一员但它与Collection、Map接口有所不同Collection接口与Map接口主要用于存储元素而Iterator主要用于迭代访问即遍历Collection中的元素因此Iterator对象也被称为迭代器。概念即Collection集合元素的通用获取方式。在取元素之前先要判断集合中有没有元素如果有就把这个元素取出来继续在判断如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。public Iterator iterator(): 获取集合对应的迭代器用来遍历集合中的元素的。Iterator接口的常用方法如下public E next():返回迭代的下一个元素。public boolean hasNext():如果仍有元素可以迭代则返回 true方法演示public class IteratorDemo { public static void main(String[] args) { // 使用多态方式 创建对象 CollectionString coll new ArrayListString(); // 添加元素到集合 coll.add(串串星人); coll.add(吐槽星人); coll.add(汪星人); //遍历 //使用迭代器 遍历 每个集合对象都有自己的迭代器 IteratorString it coll.iterator(); // 泛型指的是 迭代出 元素的数据类型 while(it.hasNext()){ //判断是否有迭代元素 String s it.next();//获取迭代出的元素 System.out.println(s); } } }2.2 增强for增强for循环(也称for each循环)是JDK1.5以后出来的一个高级for循环专门用来遍历数组和集合的。它的内部原理其实是个Iterator迭代器所以在遍历的过程中不能对集合中的元素进行增删操作。格式for(元素的数据类型 变量 : Collection集合or数组){//写操作代码}例遍历数组public class NBForDemo1 { public static void main(String[] args) { int[] arr {3,5,6,87}; //使用增强for遍历数组 for(int a : arr){//a代表数组中的每个元素 System.out.println(a); } } }遍历集合public class NBFor { public static void main(String[] args) { CollectionString coll new ArrayListString(); coll.add(小河神); coll.add(老河神); coll.add(神婆); //使用增强for遍历 for(String s :coll){//接收变量s代表 代表被遍历到的集合元素 System.out.println(s); } } }三、泛型3.1 泛型基础了解集合中是可以存放任意对象的只要把对象存储集合后那么这时他们都会被提升成Object类型。当我们在取出每一个对象并且进行相应的操作这时必须采用类型转换。例public class GenericDemo { public static void main(String[] args) { Collection coll new ArrayList(); coll.add(abc); coll.add(yaorange); coll.add(5);//由于集合没有做任何限定任何类型都可以给其中存放 Iterator it coll.iterator(); while(it.hasNext()){ //需要打印每个字符串的长度,就要把迭代出来的对象转成String类型 String str (String) it.next(); System.out.println(str.length()); } } }程序在运行时发生了问题java.lang.ClassCastException。为什么会发生类型转换异常呢我们来分析下由于集合中什么类型的元素都可以存储。导致取出时强转引发运行时 ClassCastException。怎么来解决这个问题呢Collection虽然可以存储各种对象但实际上通常Collection只存储同一类型对象。例如都是存储字符串对象。因此在JDK5之后新增了泛型(Generic)语法让你在设计API时可以指定类或方法支持泛型这样我们使用API的时候也变得更为简洁并得到了编译时期的语法检查。泛型可以在类或方法中预支地使用未知的类型。使用泛型的好处将运行时期的ClassCastException转移到了编译时期变成了编译失败。避免了类型强转的麻烦例public class GenericDemo2 { public static void main(String[] args) { CollectionString list new ArrayListString(); list.add(abc); list.add(yaorange); // list.add(5);//当集合明确类型后存放类型不一致就会编译报错 // 集合已经明确具体存放的元素类型那么在使用迭代器的时候迭代器也同样会知道具体遍历元素类型 IteratorString it list.iterator(); while(it.hasNext()){ String str it.next(); //当使用IteratorString控制元素类型后就不需要强转了。获取到的元素直接就是String类型 System.out.println(str.length()); } } }3.2 泛型的定义与使用泛型用来灵活地将数据类型应用到不同的类、方法、接口当中。将数据类型作为参数进行传递。定义和使用含有泛型的类格式修饰符 class 类名代表泛型的变量 { }class ArrayListE{ public boolean add(E e){ } public E get(int index){ } .... }使用泛型 即什么时候确定泛型。在创建对象的时候确定泛型例如ArrayListString list new ArrayListString();此时变量E的值就是String类型,那么我们的类型就可以理解为class ArrayListString{ public boolean add(String e){ } public String get(int index){ } ...}再例如ArrayListInteger list new ArrayListInteger();此时变量E的值就是Integer类型,那么我们的类型就可以理解为class ArrayListInteger { public boolean add(Integer e) { } public Integer get(int index) { } ... }自定义泛型类public class LMVP { //没有MVP类型在这里代表 未知的一种数据类型 未来传递什么就是什么类型 private MVP mvp; public void setMVP(MVP mvp) { this.mvp mvp; } public MVP getMVP() { return mvp; } }测试public class M { public static void main(String[] args) { // 创建一个泛型为String的类 LString my new LString(); // 调用setMVP my.setMVP(大胡子登登); // 调用getMVP System.out.println( my.getMVP()); //创建一个泛型为Integer的类 LInteger my2 new L(); my2.setMVP(123); Integer mvp2 my2.getMVP(); System.out.println(mvp2); } }含有泛型的方法定义格式修饰符 代表泛型的变量 返回值类型 方法名(参数){ }例public class L{ public MVP void show(MVP mvp) { System.out.println(mvp.getClass()); } public MVP MVP show2(MVP mvp) { return mvp; } }使用格式调用方法时确定泛型的类型public class M { public static void main(String[] args) { // 创建对象 L mm new L(); // 演示看方法提示 mm.show(aaa); mm.show(123); mm.show(12.45); } }含有泛型的接口定义格式修饰符 interface接口名代表泛型的变量 { }public interface LE{ public abstract void add(E e); public abstract E getE(); }使用格式1、定义类时确定泛型的类型例如package Exec; public class S implements LString{ Override public void add(String e) { // 省略... } Override public String getE() { return null; } }此时泛型E的值就是String类型。2、始终不确定泛型的类型直到创建对象时确定泛型的类型例如package Exec; public class SE implements LE{ Override public void add(E e) { // 省略... } Override public E getE() { return null; } }确定泛型使用package Exec; public class M { public static void main(String[] args) { SString my new SString(); my.add(aa); } }3.3 泛型通配符泛型的通配符:不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。例public static void main(String[] args) { CollectionIntger list1 new ArrayListInteger(); getElement(list1); CollectionString list2 new ArrayListString(); getElement(list2); } public static void getElement(Collection? coll){} //代表可以接收任意类型通配符高级使用----受限泛型之前设置泛型的时候实际上是可以任意设置的只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限和下限。泛型的上限格式类型名称 ? extends 类 对象名称意义只能接收该类型及其子类泛型的下限格式类型名称 ? super 类 对象名称意义只能接收该类型及其父类型比如现已知Object类String 类Number类Integer类其中Number是Integer的父类public static void main(String[] args) { CollectionInteger list1 new ArrayListInteger(); CollectionString list2 new ArrayListString(); CollectionNumber list3 new ArrayListNumber(); CollectionObject list4 new ArrayListObject(); getElement1(list1); getElement1(list2);//报错 getElement1(list3); getElement1(list4);//报错 getElement2(list1);//报错 getElement2(list2);//报错 getElement2(list3); getElement2(list4); } // 泛型的上限此时的泛型?必须是Number类型或者Number类型的子类 public static void getElement1(Collection? extends Number coll){} // 泛型的下限此时的泛型?必须是Number类型或者Number类型的父类 public static void getElement2(Collection? super Number coll){}