List接口&及其子類ArrayList

System.out.print()方法默認調用了打印對象的toString()方法。數組

toString()方法通常是要重寫的ide

 

當一個父類有有參構造的時候,必定要有無參構造,由於子類繼承的時候,會默認調用父類的無參構造,若是父類沒有,那麼編譯器就會報錯。this

super指代的是直接父類spa

凡是繼承,除了private 修飾的  和 構造方法,其餘的都會有,即便不寫,默認都會有。指針

size()是集合中求元素個數的方法,等同於數組中的length()code

for(Object o:c){對象

syso(o);blog

}繼承

 

迭代:就像普通for循環同樣,i每次迭代數組中的元素,這就叫迭代。索引

迭代器Iterator  和加強for循環在集合遍歷中的區別:

加強for循環在刪除集合元素時可能會出錯,而迭代器不會。

其他的沒有什麼太大的區別,只是迭代器是集合經常使用的遍歷方法而已。

 

Math.PI  :   PI封裝在Math類中,並且用static修飾,因此能夠直接打點調用。

 

集合中存儲基本數據類型,存儲的都是包裝類。

 

迭代器是接口,可是之因此能夠建立對象,是利用它的子類來建立對象。

Iterator it =c.iterator();  it 是Iterator子類的對象。

 

在編譯器中  將代碼全部的類都寫在一頁,和分頁寫,只有訪問修飾符不一樣的區別,其他沒有區別。

利用編譯期提供的重寫equals方法時,能夠選擇要判斷哪一個屬性,不必定都要判斷,好比只判斷name  就能夠只打鉤name,而不判斷其餘的屬性。

迭代器是結合特有的遍歷方法。

 

接口就是一個約定,規範,協議。

 

迭代器中的 hasNext()方法和 next()方法要配對使用,負責可能出現空指針異常。  迭代器在集合中很是重要,要重點掌握。

 

List集合:有序可重複的集合

有序:插入順序與存儲順序相同。

例如 :

a.add(1);

a.add(9);

a.add(7);

打印出來的爲 1,9,7  而不爲  1,7,9  由於是插入順序與存儲順序相同。

 

get()方法要重點掌握

 

DT時代:數據時代

 

泛型:將運行期異常轉變爲編譯期異常

泛型:參數化類型。

<>中放入想存儲的類型。

 

集合中使用了泛型的話,就只能 存儲一種對象了,那樣的話,數據和集合的區別就是:集合封裝了方法,使用起來比較方便,數組沒有封裝方法,使用起來不方便,好比,要刪除數組中的第二個元素,要寫一個for循環,而要刪除集合中的第二個元素,只要調用方法就行了。

在JAVA1.5版本之後, int類型與Integer類型的轉換,是JAVA後臺自動運行的,這叫自動裝箱和自動拆箱。

 

集合中的經常使用方法:

remove (Object  o)  刪除o對象

    注意:默認任然調用equals方法來判斷是否包含,對於自定義來講,必須得重寫哈希方法和equals方法纔可使用。

代碼示例:

 1 public class 集合中經常使用方法 {  2 public static void main(String[] args) {  3 Collection c=new ArrayList<>();  4 // System.out.println(c);  5 //isEmpty 判斷集合是否爲空元素。  6 // System.out.println(c.isEmpty());
 7 /*c.add(1);  8 c.add(2);  9 c.add(3); 10 c.add(4); 11 c.add(5); 12 System.out.println(c); 13 System.out.println(c.isEmpty());*/
14 Collection c1=new ArrayList<>(); 15 c1.add(new P1("zs", 19)); 16 c1.add(new P1("zs1", 19)); 17 c1.add(new P1("zs2", 19)); 18 c1.remove(new P1("zs", 19)); 19 System.out.println(c1); 20 } 21 } 22 class P1{ 23 String name; 24 int age; 25 public P1() { 26 super(); 27 // TODO Auto-generated constructor stub
28 } 29 public P1(String name, int age) { 30 super(); 31 this.name = name; 32 this.age = age; 33 } 34 @Override 35 public String toString() { 36 return "P1 [name=" + name + ", age=" + age + "]"; 37 1
38 return "P1 [name=" + name + ", age=" + age + "]"; 39 } 40 @Override 41 public int hashCode() { 42 final int prime = 31; 43 int result = 1; 44 result = prime * result + age; 45 result = prime * result + ((name == null) ? 0 : name.hashCode()); 46 return result; 47 } 48 @Override 49 public boolean equals(Object obj) { 50 if (this == obj) 51 return true; 52 if (obj == null) 53 return false; 54 if (getClass() != obj.getClass()) 55 return false; 56 P1 other = (P1) obj; 57 if (age != other.age) 58 return false; 59 if (name == null) { 60 if (other.name != null) 61 return false; 62 } else if (!name.equals(other.name)) 63 return false; 64 return true; 65 } 66 }

 

 

  集合的遍歷:

  1.加強for循環

  2.迭代器: Ieterator

        hashNext()  判斷是否有元素

        next()  返回該位置的元素

代碼示例:

 1 public class 集合的遍歷 {  2 public static void main(String[] args) {  3 Collection c=new ArrayList<>();// int a=5;
 4 c.add(1);  5 c.add(2);  6 c.add(3);  7 c.add(4);  8 // System.out.println(c);  9 // 遍歷集合 10 // 加強 for
11 /*for(Object o:c) { 12 System.out.println(o); 13 }*/
14 //***** 迭代器 : Iterator -- 》集合的 iterator 獲取到該接口
15 Iterator it=c.iterator(); 16 /*System.out.println(it.hasNext()); 17 System.out.println(it.next()); 18 System.out.println(it.hasNext()); 19 System.out.println(it.next());*/
20 while(it.hasNext()) { 21 Object o=it.next(); 22 System.out.println(o); 23 } 24 } 25 }

 

List集合:

List集合中的經常使用方法:  

  add(int index,E element)  在index索引位置,插入元素

  get(index):經過index返回該索引位置的元素

  set(int  index, E element)  設置index位置的元素爲參數

 

代碼示例:

 1 public class List 集合接口 {  2 public static void main(String[] args) {  3 List list=new ArrayList<>();  4 list.add(1);  5 list.add(9);  6 list.add(7);  7 // System.out.println(list);  8 // add(int index, E element) 在 index 索引位置 插入元素。  9 // list.add(1, 2); 10 // get(index) 經過 index 返回該索引位置的元素 11 // System.out.println(list.get(2)); 12 // 逆序遍歷 其中,若是爲逆序,則指針會上移,因此須要將指針置爲長度位置。
13 /*ListIterator it = list.listIterator(list.size()); 14 while(it.hasPrevious()) { 15 System.out.println(it.previous()); 16 }*/
17 System.out.println(list); 18 //set(int index, E element) 設置 iindex 位置的元素爲參數
19 list.set(1, 10); 20 System.out.println(list); 21 } 22 }

 

集合的泛型:

好處:將運行期的異常轉換爲編譯期的異常

   類型能夠轉化

參數化類型,格式: <集合中存儲的數據類型>

注意:儘管集合中能夠存儲任意類型的對象,可是通常須要加泛型,指定該集合只存儲一種數據。

代碼演示:

1 // 帶有泛型的集合
2 ArrayList<Person> a=new ArrayList<>(); 3 a.add(new Person(" 張三 ", 18)); 4 a.add(new Person(" 李四 ", 18)); 5 a.add(new Person(" 王五 ", 18)); 6 a.add(new Person("maliu", 19));

 

集合的遍歷:

代碼示例:

 1 List 集合的遍歷 :  2 *①  加強 for 循環  3 *② 迭代器 Iterator 接口  4 ③ List 特有的迭代器接口 : ListIterator  5for 循環  6 代碼演示:  7 // 帶泛型的集合的遍歷 迭代器也要加泛型。不然,數據類型爲 Object
 8 Iterator<Person> it= a.iterator();  9 while(it.hasNext()) { 10 System.out.println(it.next().name); 11 }
相關文章
相關標籤/搜索