叨逼叨两句
开始干另外一件大事了,这个事搞定就好啦~好啦
好啦~
15-3:Collection基本功能
方法
- add
- remove
- contains
- clear
- isEmpty
- size
package com.test.regex;import java.util.ArrayList;import java.util.Collection;@SuppressWarnings({"unchecked","rawtypes"})public class Demo18 { public static void main(String[] args) { Collection c = new ArrayList(); //父类引用指向子类对象 boolean b2 = c.add(true); //自动装箱了 new Boolean(true) boolean b3 = c.add(100); //自动装箱了 boolean b4 = c.add(new Student("张三",23)); boolean b5 = c.add("abc"); //add方法如果是List集合,一直都会返回true,因为List可以存储重复的元素 //add方法如果是Set集合,则当存储重复元素时,会返回false Collection c2 = new ArrayList(); boolean d1 = c2.add("a"); boolean d2 = c2.add("b"); boolean d3 = c2.add("c"); boolean d4 = c2.add("d"); System.out.println(c2); c2.remove("b"); System.out.println(c2); boolean s = c2.contains("a"); System.out.println(s); c2.clear(); System.out.println(c2.isEmpty()); System.out.println(c2.size()); }}
15-4:集合的遍历之集合转数组遍历
package com.test.regex;import java.util.ArrayList;import java.util.Collection;public class Demo19 { public static void main(String[] args) { //demo1(); Collection c = new ArrayList(); c.add(new Student("张三",23)); //Object obj = new Student("张三",23); c.add(new Student("张四",23)); c.add(new Student("张五",23)); c.add(new Student("张六",23)); Object[] arr = c.toArray(); for (int i = 0; i < arr.length; i++) { //System.out.println(arr[i]); Student s = (Student)arr[i]; System.out.println(s.getName()); } } public static void demo1() { Collection c = new ArrayList(); c.add("a"); c.add("b"); c.add("c"); c.add("d"); Object[] arr = c.toArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } }}
15-5:Collection集合的带All功能测试
- addAll
package com.test.regex;import java.util.ArrayList;import java.util.Collection;public class Demo20 { public static void main(String[] args) { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); c2.add("c"); c2.add("d"); System.out.println(c1); c1.addAll(c2); System.out.println(c1); }}
- removeAll
package com.test.regex;import java.util.ArrayList;import java.util.Collection;public class Demo20 { public static void main(String[] args) { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); System.out.println(c1); c1.removeAll(c2); System.out.println(c1); }}
- containsAll
package com.test.regex;import java.util.ArrayList;import java.util.Collection;public class Demo20 { public static void main(String[] args) { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); Collection c3 = new ArrayList(); c3.add("a"); c3.add("b"); c3.add("z"); System.out.println(c1); boolean s1= c1.containsAll(c2); boolean s2 = c1.containsAll(c3); System.out.println(s1); System.out.println(s2); }}
- retainAll
package com.test.regex;import java.util.ArrayList;import java.util.Collection;public class Demo20 { public static void main(String[] args) { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); Collection c3 = new ArrayList(); c3.add("a"); c3.add("b"); c3.add("c"); c3.add("d"); c3.add("e"); c3.add("f"); //取交集,若取完交集后,调用方法的c1的元素改变了,就返回true,若没有改变就返回false// boolean s1= c1.retainAll(c2);// System.out.println(s1);// System.out.println(c1); boolean s2 = c1.retainAll(c3); System.out.println(s2); System.out.println(c1); }}
15-6:集合的遍历之迭代器
package com.test.regex;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class Demo21 { public static void main(String[] args) { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Iterator it1 = c1.iterator(); while(it1.hasNext()){ Object a = it1.next(); System.out.println(a); } Collection c2 = new ArrayList(); c2.add(new Student("张三",23)); c2.add(new Student("张四",24)); c2.add(new Student("张五",25)); c2.add(new Student("张六",26)); Iterator it2 = c2.iterator(); while(it2.hasNext()){ Student s = (Student)it2.next(); System.out.println(s.getName()+" "+s.getAge()); } }}
15-7:无
15-8:迭代器的原理及源码解析
迭代器原理
迭代器是对集合进行遍历,而每一个集合内部的存储结构都是不同的,所以每一个集合存和取都是不一样,那么就需要在每一个类中定义hasNext()和next()方法,这样做是可以的,但是会让整个集合体系过于臃肿,迭代器是将这样的方法向上抽取出接口,然后在每个类的内部,定义自己迭代方式,这样做的好处有二,第一规定了整个集合体系的遍历方式都是hasNext()和next()方法,第二,代码有底层内部实现,使用者不用管怎么实现的,会用即可
源码解析
public Iteratoriterator() { return new Itr(); }
private class Itr implements Iterator{ int cursor; // index of next element to return int lastRet = -1; // index of last element returned; -1 if no such int expectedModCount = modCount; public boolean hasNext() { return cursor != size; } @SuppressWarnings("unchecked") public E next() { checkForComodification(); int i = cursor; if (i >= size) throw new NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) throw new ConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; } public void remove() { if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { ArrayList.this.remove(lastRet); cursor = lastRet; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }
15-9:List集合特有功能概述和测试
- add
package com.test.regex;import java.util.ArrayList;import java.util.List;public class Demo22 { public static void main(String[] args) { List list = new ArrayList();//开发一般是直接ArrayList list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); //list.add(1,"e"); //list.add(10,"f"); //list.add(4,"f"); //index <= size 都可以 System.out.println(list); }}
- remove
package com.test.regex;import java.util.ArrayList;import java.util.List;public class Demo22 { public static void main(String[] args) { List list = new ArrayList();//开发一般是直接ArrayList list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); Object obj = list.remove(1); System.out.println(obj); //移除的元素 System.out.println(list); //剩下的集合元素 }}
package com.test.regex;import java.util.ArrayList;import java.util.List;public class Demo22 { public static void main(String[] args) { List list = new ArrayList();//开发一般是直接ArrayList list = new ArrayList(); list.add(111); list.add(222); list.add(333); list.remove(111); // 删除时不会自动装箱,111被当成了索引 System.out.println(list); }}
- get
package com.test.regex;import java.util.ArrayList;import java.util.List;public class Demo22 { public static void main(String[] args) { List list = new ArrayList();//开发一般是直接ArrayList list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); //list因为有索引,可以通过get方法来遍历 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } }}
- set
package com.test.regex;import java.util.ArrayList;import java.util.List;public class Demo22 { public static void main(String[] args) { List list = new ArrayList();//开发一般是直接ArrayList list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.set(1, "z"); System.out.println(list); }}
15-10:List集合存储学生对象并遍历
package com.test.regex;import java.util.ArrayList;import java.util.List;public class Demo22 { public static void main(String[] args) { List list = new ArrayList(); list.add(new Student("张1",23)); list.add(new Student("张2",23)); list.add(new Student("张3",23)); list.add(new Student("张4",23)); list.add(new Student("张5",23)); for (int i = 0; i < list.size(); i++) { //System.out.println(list.get(i)); Student temp = (Student)list.get(i); System.out.println(temp.getName()+" "+temp.getAge()); } }}