開始幹另一件大事了,這個事搞定就好啦~好啦
好啦~java
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()); } }
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]); } } }
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); } }
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); } }
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); } }
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); } }
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()); } } }
迭代器是對集合進行遍歷,而每個集合內部的存儲結構都是不一樣的,因此每個集合存和取都是不同,那麼就須要在每個類中定義hasNext()和next()方法,這樣作是能夠的,可是會讓整個集合體系過於臃腫,迭代器是將這樣的方法向上抽取出接口,而後在每一個類的內部,定義本身迭代方式,這樣作的好處有二,第一規定了整個集合體系的遍歷方式都是hasNext()和next()方法,第二,代碼有底層內部實現,使用者不用管怎麼實現的,會用便可數組
public Iterator<E> iterator() { return new Itr(); }
private class Itr implements Iterator<E> { 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(); } }
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); } }
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); } }
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)); } } }
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); } }
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()); } } }