ArrayList是一種動(dòng)態(tài)數(shù)組,其容量可隨著我們的需要自動(dòng)進(jìn)行擴(kuò)充.
ArrayList位于System.Collections命名空間中,所以我們?cè)谑褂脮r(shí),需要導(dǎo)入此命名空間.
下面,我們還是在Student類(lèi)的基礎(chǔ)上利用ArrayList操作,從而了解ArrayList的用法
05 |
public Student(String name, int age,String hobby) |
35 |
Console.WriteLine( "大家好,我是'{0}',今年{1}歲,我喜歡'{2}'" , |
36 |
this .Name, this .Age, this .Hobby); |
編寫(xiě)測(cè)試類(lèi),了解ArrayList的方法
01 |
using System.Collections; |
03 |
public class TestStudent |
05 |
public static void main(String args []) |
08 |
ArrayList students = new ArrayList(); |
11 |
Student rose = new Student( "rose" ,25, "reading" ); |
12 |
Student jack = new Student( "jack" ,28, "singing" ); |
13 |
Student mimi = new Student( "mimi" ,26, "dancing" ); |
21 |
int number = students.Count; |
22 |
Console.WriteLine( "共有元素" + number + "個(gè)" ); |
26 |
Student stu = students[0] as Student; |
30 |
for ( int i = 0;i < students.Count;i ++) |
32 |
Student a = students[i] as Student; |
36 |
foreach (Object o in students) |
38 |
Student b = o as Student; |
45 |
students.remove(jack); |
57 |
int number = students.Capacity; |
59 |
students.TrimtoSize(); |
|