ASP.NET中的ArrayList对象是一个由包含单一数据值的数据项组成的集合。如何创建ArrayList对象以及如何对ArrayList内的各数据项内容进行排序呢,下面说一些代码示例。
创建ArrayList数据项是用Add()方法添加到ArrayList的。以下代码创建一个新的ArrayList对象,命名为mycountries,并加入4个数据项:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") end if end sub </script>
默认情况下,一个ArrayList对象包含16条记录。可以使用TrimToSize()方法限定ArrayList为其最终的实际大小:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() end if end sub </script>
还可以用Sort()方法为ArrayList按字母顺序或数字顺序排序:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() end if end sub </script>
要想按倒序排序,可在Sort()方法的后面使用Reverse()方法:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() mycountries.Reverse() end if end sub </script>
你可以试着将代码复制到你的ASP.NET页面中测试看效果。