本章概述:本章主要讲了ArrList,Collection,以及泛型在集合框架当中的使用
目录
本章概述:
一、ArrList的使用
1、ArrListDemo
二、Collection的使用
1、CollectionDemo
三、泛型的使用
1、FanXingClass
2、FanxingDemo
3、FanXingInterface
4、FanXingMethod
本章概述:
一、ArrList的使用
1、ArrListDemo
package collection.class2;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("123");
arrayList.add(123);
arrayList.add(true);
System.out.println(arrayList);
}
}
二、Collection的使用
1、CollectionDemo
package collection.class2;
import java.util.ArrayList;
import java.util.Collection;
/*
* java集合框架:
* Collection:存放的是单一值
* 特点:
* 1、可以存放不同类型的数据,而数组只能存放固定类型的数据
* 2、当使用Arraylist子类实现的时候,初始化的长度是10,当长度不够的时候会自动进行扩容操作
* api方法:
* 增加数据的方法
* add:要求必须传入的参数是Object对象,因此当写入基本数据类型的时候,包含了自动拆箱和自动装箱的过程
* addAll:添加另一个集合的元素到此集合中
*
* 删除数据的方法
* clear:只是清空集合中的元素,但是此集合对象并没有被回收
* remove:删除指定元素
* removeAll:删除集合元素
*
* 查询数据的方法
* contains:判断集合中是否包含指定的元素值
* containsAll:判断此集合中是否包含另一个集合
* isEmpty:判断集合是否等于空
* retainAll:若集合中拥有另一个集合的所有元素,返回true,否则返回false
* size:返回当前集合的大小
*
* //集合转数组的操作
* toArray:将集合转换成数组
* */
public class CollectionDemo {
public static void main(String[] args) {
Collection collection = new ArrayList();
collection.add(1); //new Integer(1)
collection.add("123"); // new String("123")
collection.add(true); //new Boolean(true)
System.out.println(collection);
Collection collection1 = new ArrayList();
collection1.add("abc");
collection1.add(666);
collection1.add(false);
collection.addAll(collection1);
// collection.clear();
System.out.println(collection);
System.out.println(collection.contains("abc"));
System.out.println(collection.isEmpty());
// collection.remove(666);
// System.out.println(collection);
// collection.removeAll(collection1);
// System.out.println(collection);
// System.out.println(collection1.retainAll(collection));
// System.out.println(collection);
System.out.println(collection.size());
Object[] objects = collection.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
}
}
三、泛型的使用
1、FanXingClass
package collection.class2;
public class FanXingClass<A> {
private String name;
private A a;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
public void show(){
System.out.println("name: " + name + " A:" + a);
}
public void set(A a){
System.out.println("这是set方法中的A" + a);
}
public A get(){
return a;
}
}
2、FanxingDemo
package collection.class2;
import java.util.ArrayList;
import java.util.List;
/**
* 当做一些集合的统一操作的时候,需要保证集合的类型是统一的,此时需要泛型来进行限制
* 优点:
* 1、数据安全
* 2、获取数据时效率比较高
* 给集合中的元素设置相同的类型就是泛型的基本需求
* 使用:
* 在定义对象的时候,通过<>中设置合理的类型来进行实现
* 泛型的高阶应用:
* 1、泛型类
* 在定义类的时候在类名的后面添加<E,K,V,A,B>,起到占位的作用,类中的方法的返回值类型和属性的类型都可以使用
* 2、泛型接口
* 在定义接口的时候,在接口的名称后添加<E,K,V,A,B>,
* 1、子类在进行实现的时候,可以不填写泛型的类型,此时在创建具体的子类对象的时候才决定使用什么类型
* 2、子类在实现泛型接口的时候,只在实现父类的接口的时候指定父类的泛型类型即可,此时,测试方法中的泛型类型必须要跟子类保持一致
* 3、泛型方法
* 在定义方法的时候,指定方法的返回值和参数是自定义的占位符,可以是类名中的T,也可以是自定义的Q,只不过在使用Q的时候需要使用<
* Q>定义在返回值的前面
* 4、泛型的上限(工作中不用)
* 如果父类确定了,所有的子类都可以直接使用
* 5、泛型的下限(工作中不用)
* 如果子类确定了,子类的所有父类都可以直接传递参数使用
*
*
*/
public class FanXingDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1"); //new Integer(1)
list.add("123"); // new String("123")
list.add("true"); //new Boolean(true)
//
// //遍历集合
// for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i));
// }
//
// System.out.println("***************************************");
// for(String o : list){
String str = (String)o;
// System.out.println(o);
// }
// FanXingClass<String> fanXingClass = new FanXingClass<String>();
// fanXingClass.setNum(1);
// fanXingClass.setA("123");
// fanXingClass.show();
//
// FanXingClass<Integer> fanXingClass2 = new FanXingClass<Integer>();
// fanXingClass2.setNum(2);
// fanXingClass2.setA(1234);
// fanXingClass2.show();
//
// FanXingClass<Person> fanXingClass3 = new FanXingClass<Person>();
// fanXingClass3.setNum(3);
// fanXingClass3.setA(new Person("小白",18));
// fanXingClass3.show();
//
// FanXingClass<String> fanXingClass4 = new FanXingClass<String>();
// fanXingClass4.setNum(4);
// fanXingClass4.set("666");
// fanXingClass4.setA("8888");
// System.out.println(fanXingClass4.get());
// fanXingClass4.show();
// FanXingInterface fanXingInterface = new FanXingInterfaceImpl();
// fanXingInterface.test2("666");
// System.out.println(fanXingInterface.test());
FanXingMethod<String> fanXingMethod = new FanXingMethod<String>();
fanXingMethod.setT("666");
fanXingMethod.show(123);
fanXingMethod.show(true);
}
}
3、FanXingInterface
package collection.class2;
public interface FanXingInterface<B> {
public abstract void test(B b);
public abstract B test2();
}
4、FanXingMethod
package collection.class2;
public class FanXingMethod<T> {
private int id;
private T t;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public <Q> void show(Q q){
System.out.println(t);
System.out.println(q);
}
}