java集合 之泛型 删除方法 排序 复合嵌套
来源:互联网 发布:淘宝第三方支付平台 编辑:程序博客网 时间:2024/05/20 05:45
泛型
1.增加了代码安全性
2.省去了强转换的麻烦
泛型类 是在创建 对象的时候 给泛型缚类型
// 声明一个集合 保存adbcd 迭代器遍历 // 泛型:声明集合中保存的元素什么类型 ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("a"); arrayList.add("b"); arrayList.add("c"); arrayList.add("d"); ListIterator<String> listIterator = arrayList.listIterator(); while(listIterator.hasNext()) { String next = listIterator.next(); System.out.println(next); }// 逆向遍历// 判断前一个元素是否有值 while(listIterator.hasPrevious()) { // 取除前一个元素 String previous = listIterator.previous(); System.out.println(previous); }
//需求 集合中保存了3个学生对象迭代器遍历(使用泛型) // 在jdk1.7菱形泛型 // 后面泛型 可以不写 如果不写 默认和声明泛型一致 // 如果你前后泛型 都写 必须保持一致 ArrayList<Student> arrayList = new ArrayList<Student>(); arrayList.add(new Student("王龙",39)); arrayList.add(new Student("越哥",20)); arrayList.add(new Student("越爷",24)); ListIterator<Student> listIterator = arrayList.listIterator(); while(listIterator.hasNext()) { Student next = listIterator.next(); System.out.println(next.getName()); }
ArrayList<Student> list = new ArrayList<>(); list.add(new Student("王龙",39)); list.add(new Student("王龙",39)); list.add(new Student("王龙",39));// Person person =(Person)list.listIterator().next();// System.out.println(person.getName()); // 泛型 可以 将运行时 的错误 转到编译期// Worker next = (Worker)list.iterator().next(); Worker.work();
泛型 的表示<任意的大写字母>
类的泛型
public class Worker<T> { private T t; private String name; public String toString() { return "T [T=" + t + "]"; } public Worker() { super(); } public Worker(T t) { super(); this.t = t; } public T getT() { return t; } public void setT(T t) { this.t = t; } //泛型成员方法 //除了T类型 我这里咳哟不尅写别的类型 // 这里W没有被符类型 // 咱们需要表示 出来 再调用这个方法时候 创建 W类型 public<W> void sayHi(W w) { System.out.println(w); } public static void work() { System.out.println("我是工人,我很开心"); } //静态方法 自己的泛型 // 静态方法自己的泛型 public static<E> void print(E e) { }}
泛型类的调用
Worker<String > worker = new Worker<>(); worker.setT("haha"); worker.sayHi("SB"); System.out.println();
泛型接口
//直接在接口名 后面声明泛型interface InterA<T>{ void show(T t);}class InterAImpl implements InterA<String>{ @Override public void show(String t) { // TODO Auto-generated method stub System.out.println(t); }}
泛型安全性的例子
// 创建两个数组 一个春Person //Student素组里村两人 //把Student素组全添加到Person中 ArrayList<Person> list1 = new ArrayList<>(); ArrayList<Student> list2 = new ArrayList<>(); list1.add(new Person("sb",12)); list1.add(new Person("sn",13)); list2.add(new Student("w",13)); list2.add(new Student("s",14)); list2.addAll(list1);//报错 System.out.println(list2);
泛型规定了集合里能存放元素的类型
所以报错
泛型通配符
? extends E
其中问号是子类 E是父类
向下限定 只允许 子类或者孙子类
Collection
多参数测试
// 遍历int数组的方法 //下列方法还能不能添加int类型参数 // 如果多参数 那么int....num 必须放在最后 public static void print(int a,int...num) { for (int i = 0; i < num.length; i++) { System.out.println(i); } }
asList方法
asList 把数组转为List
//asList 把数组转为List //这里是把整个数组当成一个元素保存到集合中了 // 因为集合中只能保存对象类型 // 数组中元素不能自动装箱 int[] array = {1,2,3,4,5}; List<int[]> asList = Arrays.asList(array); System.out.println(asList); // 直接使用基本数据类型 包装类 Integer[] newArray = new Integer[]{12,11,3,4}; List<Integer> asList2 = Arrays.asList(newArray); System.out.println(asList2);
特别注意点
需求:
字符串数组转化为集合 并在集合中添加”liangliang”
// 数组转化为集合不能对其进行添加 和删除的操作 // 不支持添加删除有何意义 // 意义在于 可以使用集合中其他方法 String[] strings = {"wanglong","wangning"}; List<String> asList = Arrays.asList(strings); asList.add("liangliang");//报错 System.out.println(asList);
删除的3种方式(遍历的三种方式)
1.
ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("d"); //如果集合中有"b"删除 for (int i = 0; i <list.size(); i++) { if (list.get(i).equals("b")) { // i-- 表示每次删除后 都把角标跳一次 // 为了保证 删除后也能便利到数组每一个元素 // ListIterator list.remove(i--); } } System.out.println(list); }
2
ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("d"); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { if(iterator.next().equals("b")) { //让迭代器去删除 iterator.remove(); } } System.out.println(list);
3.
ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("b"); list.add("c"); //增强for循环遍历 // 遍历 // 冒号后 是你要遍历的容器 // 冒号前 是你要便利的容器中的每个元素 // 增强for循环底层 使用迭代器实现的 // 一般只用来打印 不要做删除操作 for (String string : list) { System.out.println(string); }
集合排序
ArrayList<Student> list = new ArrayList<>(); list.add(new Student("a",20)); list.add(new Student("d",30)); list.add(new Student("c",50)); list.add(new Student("e",100)); list.add(new Student("b",40)); for (int i = 0; i < list.size()-1; i++) { for (int j = 0; j < list.size()-1-i; j++) { if(list.get(j).getName().compareTo(list.get(j+1).getName())>1) { Collections.swap(list, j, j+1);//调用系统的交换方法 } } } System.out.println(list);
Collections中的sort方法
这个方法是系统给你的排序方法
但是按什么排序 系统不知道要咋排序
这时系统给你一个接口让你去实现这个接口
接口中你要写 排序规则 (回掉思想)
集合中盛放的时神么对象 就让该对象趋势线这个接口(拍谁谁实现)
使用Collections中sort方法排序步骤
1.让集合中被排序的对象的类 去实现Comparable接口
2.实现接口中抽象方法(排序是什么规则的方法)
3.编写排序规则
4.调用Collections.sort测试
ArrayList<Student> list = new ArrayList<>(); list.add(new Student("a",20)); list.add(new Student("d",30)); list.add(new Student("c",50)); list.add(new Student("e",100)); list.add(new Student("b",40)); //调用系统方法 // (底层调用了实现接口中的排序方法) Collections.sort(list); System.out.println(list);
重写代码
public int compareTo(Student o) { // TODO Auto-generated method stub // Comparable接口中的抽象方法 // 这个抽象方法 让你去写排序规则// System.out.println("我执行了"); return (int)this.getName().charAt(0)-(int)o.getName().charAt(0); }
集合嵌套
阅读全文