java中對象數組的小例題


/*
* 我有5個學生,請把這個5個學生的信息存儲到數組中,並遍歷學生數組,獲取得到每一個學生的信息。
*   學生類:Student
*      成員變量:name,age
*      構造方法:無參,帶參
*      成員方法:getXxx()/setXxx()
*   存儲學生的數組?自己想想應該是什么樣子的?
* 分析:
*    A:創建學生類。
*    B:創建學生數組(對象數組)。
*    C:創建5個學生對象,並賦值。
*    D:把C步驟的元素,放到學生數組中。
*    E:遍歷學生數組。
*/

示例代碼如下:

 1 package cn.itcast_01;
 2 
 3 public class Student {
 4     // 成員變量
 5     private String name;
 6     private int age;
 7 
 8     // 構造方法
 9     public Student() {
10         super();
11     }
12 
13     public Student(String name, int age) {
14         super();
15         this.name = name;
16         this.age = age;
17     }
18 
19     // 成員方法
20     // getXxx()/setXxx()
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public int getAge() {
30         return age;
31     }
32 
33     public void setAge(int age) {
34         this.age = age;
35     }
36 
37     @Override
38     public String toString() {
39         return "Student [name=" + name + ", age=" + age + "]";
40     }
41 }
Student.java
 1 package cn.itcast_01;
 2 
 3 /*
 4  * 我有5個學生,請把這個5個學生的信息存儲到數組中,並遍歷學生數組,獲取得到每一個學生的信息。
 5  *         學生類:Student
 6  *             成員變量:name,age
 7  *             構造方法:無參,帶參
 8  *             成員方法:getXxx()/setXxx()
 9  *         存儲學生的數組?自己想想應該是什么樣子的?
10  * 分析:
11  *         A:創建學生類。
12  *         B:創建學生數組(對象數組)。
13  *         C:創建5個學生對象,並賦值。
14  *         D:把C步驟的元素,放到學生數組中。
15  *         E:遍歷學生數組。
16  */
17 public class ObjectArrayDemo {
18     public static void main(String[] args) {
19         // 創建學生數組(對象數組)。
20         Student[] students = new Student[5];
21         
22         // 遍歷新創建的學生數組。
23         for (int x = 0; x < students.length; x++) {
24             System.out.println(students[x]);
25         }
26         System.out.println("---------------------");
27 
28         // 創建5個學生對象,並賦值。
29         Student s1 = new Student("林青霞", 27);
30         Student s2 = new Student("風清揚", 30);
31         Student s3 = new Student("劉意", 30);
32         Student s4 = new Student("趙雅芝", 60);
33         Student s5 = new Student("王力宏", 35);
34 
35         // 把C步驟的元素,放到學生數組中。
36         students[0] = s1;
37         students[1] = s2;
38         students[2] = s3;
39         students[3] = s4;
40         students[4] = s5;
41 
42          // 看到很相似,就想用循環改,把C步驟的元素,放到學生數組中。
43 //         for (int x = 0; x < students.length; x++) {
44 //             students[x] = s + "" + (x + 1); // 拼完之后是一個字符串了。
45 //         }
46          // 這個是有問題的。
47 
48         // 遍歷賦值后的學生數組。用重寫toString()方法
49         for (int x = 0; x < students.length; x++) {
50             // 重寫toString()方法,注意:一個方法寫定之后就不要再去改變了。因為改來改去的還不如重新寫個方法呢?
51             System.out.println(students[x]); 
52         }
53         System.out.println("---------------------");
54         
55         // 遍歷賦值后的學生數組,用getXxx()方法
56         for (int x = 0; x < students.length; x++) {            
57             // 因為學生數組的每一個元素都是一個學生。
58             Student s = students[x];
59             System.out.println(s.getName()+"---"+s.getAge());
60         }
61     }
62 }
ObjectArrayDemo.java
 1 null
 2 null
 3 null
 4 null
 5 null
 6 ---------------------
 7 Student [name=林青霞, age=27]
 8 Student [name=風清揚, age=30]
 9 Student [name=劉意, age=30]
10 Student [name=趙雅芝, age=60]
11 Student [name=王力宏, age=35]
12 ---------------------
13 林青霞---27
14 風清揚---30
15 劉意---30
16 趙雅芝---60
17 王力宏---35
輸出結果如下:

 


免責聲明!

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



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