韩世雷程序员 2019-06-21
package com.test.regex; import java.util.ArrayList; import java.util.Iterator; public class Demo28 { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(110); list.add(true); list.add(new Student("张三",23)); Iterator it = list.iterator(); while(it.hasNext()){ // System.out.println(it.next()); //遍历集合中的元素并打印没有问题,但是若想要使用元素中特有的方法,则必须强制转换 // Student stu = (Student)it.next(); //这样强制转换出错了,明明编译期没有问题啊?那是因为集合混入了除Student以外的其它类型 // System.out.println(stu.getName()+" "+stu.getAge()); } } }
package com.test.regex; import java.util.ArrayList; import java.util.Iterator; public class Demo28 { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<Student>(); //list.add(110); //list.add(true); list.add(new Student("张三",23)); list.add(new Student("张四",33)); Iterator<Student> it = list.iterator(); while(it.hasNext()){ Student stu = it.next(); System.out.println(stu.getName()+" "+stu.getAge()); } } }
package com.test.regex; import java.util.ArrayList; import java.util.Iterator; public class Demo28 { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<Student>(); //list.add(110); //list.add(true); list.add(new Student("张三",23)); list.add(new Student("张四",33)); Iterator<Student> it = list.iterator(); while(it.hasNext()){ //Student stu = it.next(); System.out.println(it.next().getName()+" "+it.next().getAge()); } } }
package com.test.regex; import java.util.ArrayList; import java.util.Iterator; public class Demo28 { public static void main(String[] args) { //ArrayList<Object> list2 = new ArrayList<Student>(); //前后泛型类型不一致,不可以 //ArrayList<Object> list2 = new ArrayList<>(); //菱形泛型,前面写了后面就不写了,默认与前面一致,JDK1.7以后的新特性 ArrayList<Object> list2 = new ArrayList<>(); //设置泛型为Object没有意义,这样的集合 list2.add("aaa"); list2.add(true); demo01(); }
package com.test.regex; import java.util.ArrayList; import java.util.Iterator; public class Demo29 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); Iterator<String> it = list.iterator(); for(;it.hasNext();){ System.out.println(it.next()); } } }
package com.test.regex; import java.util.ArrayList; import java.util.Iterator; public class Demo29 { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<>(); list.add(new Student("kk5",65)); list.add(new Student("kk4",55)); list.add(new Student("kk3",35)); list.add(new Student("kk2",45)); list.add(new Student("kk1",15)); Iterator<Student> it = list.iterator(); for(;it.hasNext();){ Student stu = it.next(); System.out.println(stu.getName()+" "+stu.getAge()); } } }
package com.test.demo30; public class Demo30 { public static void main(String[] args) { Tool t = new Tool(); t.setObj(new Student()); Worker w = (Worker)t.getObj(); System.out.println(w); } }
package com.test.demo30; public class Tool { private Object obj; public Object getObj() { return obj; } public void setObj(Object obj) { this.obj = obj; } }
package com.test.demo30; public class Person { private String name; private int age; public Person() { super(); // TODO Auto-generated constructor stub } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
package com.test.demo30; public class Student extends Person { public Student() { // TODO Auto-generated constructor stub } public Student(String name, int age) { super(name, age); // TODO Auto-generated constructor stub } }
package com.test.demo30; public class Worker extends Person { public Worker() { // TODO Auto-generated constructor stub } public Worker(String name, int age) { super(name, age); // TODO Auto-generated constructor stub } }
package com.test.demo30; public class Tool<Q> { private Q obj; public Q getObj() { return obj; } public void setObj(Q obj) { this.obj = obj; } }
package com.test.demo30; public class Demo30 { public static void main(String[] args) { Tool<Student> t = new Tool<>(); t.setObj(new Student()); Worker w = (Worker)t.getObj(); System.out.println(w); } }
package com.test.demo30; public class Tool<Q> { private Q q; public Q getObj() { return q; } public void setObj(Q q) { this.q = q; } public void show(Q q){ System.out.println(q); } public <T> void show(T t){ System.out.println(t); } }
package com.test.demo30; public class Demo30 { public static void main(String[] args) { Tool<String> t = new Tool(); t.show("abc"); } }
package com.test.demo30; public class Tool<Q> { private Q q; public Q getObj() { return q; } public void setObj(Q q) { this.q = q; } public<W> void show(W w){ System.out.println(w); } }
package com.test.demo30; public class Demo30 { public static void main(String[] args) { Tool<String> t = new Tool(); t.show(true); t.show(new Integer(111)); t.show(111); } }
package com.test.demo30; public class Tool<Q> { private Q q; public Q getObj() { return q; } public void setObj(Q q) { this.q = q; } public<W> void show(W w){ System.out.println(w); } public static <Q> void print(Q q){ System.out.println(q); } }
package com.test.demo30; public class Demo30 { public static void main(String[] args) { Tool.print("a"); } }
package com.test.regex; public class Demo31 { public static void main(String[] args) { } } interface Inter<T> { public void show(T t); } //第一种实现接口的方式,推荐这种 /*class Demo001 implements Inter<String> { @Override public void show(String t) { System.out.println(t); } }*/ //第二种实现接口的方式,不推荐这种,实现接口没必要让类也加上泛型 class Demo002<T> implements Inter<String>{ @Override public void show(String t) { // TODO Auto-generated method stub } }
package com.test.regex; import java.util.ArrayList; import java.util.List; public class Demo32 { public static void main(String[] args) { List<?> list = new ArrayList<Integer>(); // 当右边泛型不确定时,左边可以用? List<?> list2 = new ArrayList<String>(); List<String> list3 = new ArrayList<String>(); //List<E> list = new ArrayList<Integer>(); //编译不通过 } }
package com.test.demo30; import java.util.ArrayList; public class Demo32 { public static void main(String[] args) { ArrayList<Person> list1 = new ArrayList<>(); list1.add(new Person("张三",23)); list1.add(new Person("张四",24)); list1.add(new Person("张五",25)); ArrayList<Student> list2 = new ArrayList<>(); list2.add(new Student("赵四",24)); list2.add(new Student("赵五",34)); list1.addAll(list2); System.out.println(list1); //list2.addAll(list1); //编译不通过 System.out.println(list2); } }
package com.test.demo30; public class Student extends Person { public Student() { // TODO Auto-generated constructor stub } public Student(String name, int age) { super(name, age); // TODO Auto-generated constructor stub } }
package com.test.demo30; import java.util.ArrayList; public class Demo33 { public static void main(String[] args) { int[] arr = {11,22,33,44,55}; for(int i : arr){ System.out.println(i); } ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); list.add("f"); for (String string : list) { System.out.println(string); } } }
package com.test.demo30; import java.util.ArrayList; public class Demo33 { public static void main(String[] args) { ArrayList<Student> list = new ArrayList<Student>(); list.add(new Student("zzz",22)); list.add(new Student("z1",22)); list.add(new Student("z22z",22)); list.add(new Student("z3",22)); list.add(new Student("z4z",22)); for (Student stu : list) { System.out.println(stu); } } }
package com.test.demo30; import java.util.ArrayList; import java.util.Iterator; public class Demo33 { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); list.add("b"); list.add("d"); //1. 普通for循环,可以删除,但是要在删除后将游标减一 /* for(int i = 0; i < list.size(); i++){ if("b".equals(list.get(i))){ list.remove(i--); } }*/ // 2. 迭代器,可以删除,但是不可使用list的删除方法,而应该使用Iterator的删除方法,否则将出现并发修改异常 /*for(Iterator<String> it = list.iterator();it.hasNext();){ if("b".equals(it.next())){ //list.remove("b"); it.remove(); } }*/ // 3. 增强for循环, 由于底层是迭代器,只可遍历,不可删除。 for (String string : list) { if("b".equals(string)){ // list.remove("b"); } } System.out.println(list); } }
import 包名.类名
静态导入:import static 包名.类名.方法名
package com.test.demo30; public class Demo34 { public static void main(String[] args) { int[] arr = {11,22,33,44,55}; //print(arr); print(11,22,33,44,55,66); System.out.println("----------------"); print(); } /*public static void print(int[] arr){ for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } }*/ public static void print(int ... arr){ //可变参数其实就是一个数组 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
package com.test.demo30; public class Demo34 { public static void main(String[] args) { print(11,22,33,44,55,66); } //public static void print(int ... arr,int x){ //这样设置参数是不行的 public static void print(int x,int ... arr){ //可变参数其实就是一个数组 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
package com.test.demo30; import java.util.Arrays; import java.util.List; public class Demo35 { /** * @param args * 数组转换为集合 */ public static void main(String[] args) { String[] arr = {"a","b","c"}; List<String> list = Arrays.asList(arr); list.add("d"); System.out.println(list); //数组转换成集合虽然不能增加或减少元素,但是可以用集合的思想操作数组,也就是说可以使用其他集合中的方法 } }
package com.test.demo30; import java.util.Arrays; import java.util.List; public class Demo35 { /** * @param args * 数组转换为集合 */ public static void main(String[] args) { //demo01(); int[] arr = {11,22,33,44,55}; List<int[]> list = Arrays.asList(arr); System.out.println(list); } public static void demo01() { String[] arr = {"a","b","c"}; List<String> list = Arrays.asList(arr); //list.add("d"); System.out.println(list); //数组转换成集合虽然不能增加或减少元素,但是可以用集合的思想操作数组,也就是说可以使用其他集合中的方法 } }
public static void main(String[] args) { Integer[] arr = {11,22,33,44,55}; List<Integer> list = Arrays.asList(arr); System.out.println(list); }
集合转数组【加泛型】
package com.test.demo30; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo35 { /** * @param args * 数组转换为集合 */ public static void main(String[] args) { ArrayList<String> a = new ArrayList<>(); a.add("a"); a.add("b"); a.add("c"); a.add("d"); // String[] arr = a.toArray(new String[0]); // String[] arr = a.toArray(new String[1]); String[] arr = a.toArray(new String[10]); for (String string : arr) { System.out.println(string); } }
package com.test.demo30; import java.util.ArrayList; public class Demo36 { public static void main(String[] args) { ArrayList<ArrayList<Person>> list = new ArrayList<>(); //学科集合 ArrayList<Person> first = new ArrayList<>(); //班级集合 first.add(new Person("kk",30)); //每一个人 first.add(new Person("j",30)); first.add(new Person("gg",30)); ArrayList<Person> second = new ArrayList<>(); second.add(new Person("a",30)); second.add(new Person("b",30)); second.add(new Person("c",30)); list.add(first); list.add(second); for (ArrayList<Person> a : list) { for(Person p: a){ System.out.println(p); } } } }
叨逼叨两句收拾收拾~18-11:Collection中的常见方法sortpackage com.test.demo001;System.out.println; //根据默认排序结果获取集合中的最大值