Java 對象數組


java對象數組的概述和使用

 1 public class Student  2 { 3 // 成員變量 4 private String name; 5 private int age; 6 7 // 構造方法 8 public Student()  9  { 10 super(); 11 } 12 13 public Student(String name, int age) 14  { 15 super(); 16 this.name = name; 17 this.age = age; 18 } 19 20 // 成員方法 21 // getXxx()/setXxx() 22 public String getName() 23  { 24 return name; 25 } 26 27 public void setName(String name) 28  { 29 this.name = name; 30 } 31 32 public int getAge() 33  { 34 return age; 35 } 36 37 public void setAge(int age) 38 { 39 this.age = age; 40 } 41 42 @Override 43 public String toString() 44 { 45 return "Student [name=" + name + ", age=" + age + "]"; 46 } 47 }
 1 /**  2 把5個學生的信息存儲到數組中,並遍歷數組,獲取得到每一個學生信息。  3 * 學生:Student  4 * 成員變量:name,age  5 * 構造方法:無參,帶參  6 * 成員方法:getXxx()/setXxx()  7 * 分析:  8 * A:創建學生類。  9 * B:創建學生數組(對象數組)。 10 * C:創建5個學生對象,並賦值。 11 * D:把C步驟的元素,放到數組中。 12 * E:遍歷學生數組。 13 * */ 14 15 public class Practice 16 { 17 public static void main(String[] args) 18 { 19 // 創建學生數組(對象數組)。 20 Student[] students = new Student[5]; 21 // for (int x = 0; x < students.length; x++) 22 // { 23 // System.out.println(students[x]); 24 // } 25 // System.out.println("---------------------"); 26 27 // 創建5個學生對象,並賦值。 28 Student s1 = new Student("小明", 27); 29 Student s2 = new Student("小紅", 30); 30 Student s3 = new Student("小強", 30); 31 Student s4 = new Student("旺財", 12); 32 Student s5 = new Student("張三", 35); 33 34 // 將對象放到數組中。 35 students[0] = s1; 36 students[1] = s2; 37 students[2] = s3; 38 students[3] = s4; 39 students[4] = s5; 40 41 // 遍歷 42 for (int x = 0; x < students.length; x++) 43 { 44 //System.out.println(students[x]); 45 Student s = students[x]; 46 System.out.println(s.getName()+"---"+s.getAge()); 47 } 48 } 49 }

15.02 對象數組的內存圖解

15.03 集合的由來及與數組的區別

集合類的由來:面向對象語言對事物的體現都是以對象的形式,所以為了方便對多個對象的操作,Java就提供了集合類。

數組和集合類同的區別:

數組可以存儲同一種類型的基本數據也可以存儲同一種類型的對象,但長度是固定的

集合只可以存儲不同類型的對象,長度是可變的

集合類的特點:集合只用於存儲對象,集合長度是可變的,集合可以存儲不同類型的對象。

15.04 集合的繼承體系圖解

集合容器因為內部的數據結構不同,有多種具體容器,根據共性內容不斷的向上抽取,就形成了集合框架。

框架的頂層Collection接口

15.05 Collection集合的功能概述

Collection 層次結構中的根接口。Collection 表示一組對象,這些對象也稱為 collection 的元素。一些 collection 允許有重復的元素,而另一些則不允許。一些 collection 是有序的,而另一些則是無序的。JDK 不提供此接口的任何直接實現:它提供更具體的子接口(如 Set 和 List)實現。此接口通常用來傳遞 collection,並在需要最大普遍性的地方操作這些 collection。

15.06 Collection集合的基本功能測試

成員方法:

1.  boolean add(Ee):確保此 collection 包含指定的元素(可選操作)。

2.  boolean remove(Objecto):從此 collection 中移除指定元素的單個實例,如果存在的話(可選操作)。

3.  void clear():移除此 collection 中的所有元素(可選操作)。

4.  boolean contains(Objecto):如果此 collection 包含指定的元素,則返回 true。

5.  boolean isEmpty():如果此 collection 不包含元素,則返回 true。

6.  int size():返回此 collection 中的元素數。

例:

 1 // 創建集合對象 2 // Collection c = new Collection(); //錯誤,因為接口不能實例化 3 Collection c = new ArrayList(); 4 c.add("hello"); 5 c.add("world"); 6 c.add("java"); 7 // c.clear();//移除所有元素 8 // System.out.println("remove:" + c.remove("hello"));//移除一個元素 9 // System.out.println("remove:" + c.remove("javaee")); 10 // 判斷集合中是否包含指定的元素 11 System.out.println("contains:"+c.contains("hello"));//contains:true 12 System.out.println("contains:"+c.contains("android"));//contains:false 13 //判斷集合是否為空 14 System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false 15 //元素的個數 16 System.out.println("size:"+c.size());//size:3 17 System.out.println("c:" + c);//c:[hello, world, java]

15.07 Collection集合的高級功能測試

成員方法:

1.  boolean addAll(Collection<? extends E> c):

將指定 collection 中的所有元素都添加到此 collection 中(可選操作)。

2.  boolean removeAll(Collection<?> c):

移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。

3.  boolean containsAll(Collection<?> c):

如果此 collection 包含指定 collection 中的所有元素,則返回 true。

4.  boolean retainAll(Collection<?> c):

僅保留此 collection 中那些也包含在指定 collection 的元素(可選操作)。換句話說,移除此 collection 中未包含在指定 collection 中的所有元素。

例:

c1.addAll(c2);//將c2集合中的所有元素添加到c1集合中,c1變c2不變 c1.removeAll(c2);//將c1集合中與c2集合相同的所有元素刪除,只要有一個相同的就返回true c1.containsAll(c2);//判斷c1集合中的元素是否包含c2中的全部元素,全部包含則返回true c1.retainAll(c2);//將c1集合中與c2集合相同的元素保留,刪除其他元素,返回值表示c1集合是否發生變化,發生變化返回true,沒有變化返回false

15.08 集合的遍歷之集合轉數組遍歷

Object[] toArray():返回包含此 collection 中所有元素的數組。

例:

 1 public class Practice  2 { 3 public static void main(String[] args) 4 { 5 // 創建集合 6 Collection c = new ArrayList(); 7 c.add("hello"); 8 c.add("world"); 9 c.add("java"); 10 11 Object[] objs = c.toArray(); 12 for (int i = 0; i < objs.length; i++) 13 { 14 //向下轉為String類型 15 String s = (String)objs[i]; 16 System.out.println(s+":"+s.length()); 17 } 18 } 19 }

運行結果:

hello:5 world:5 java:4


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM