数组列简介

简介: 数组列(ArrayList)与数组类似,但在给它增加元素时,能够自动扩展,而数组则有固定大小,其属于System.Collections命名空间的一部分。 它有Add,AddRange,Insert,InsertRange,Sort等方法,可理解为动态数组。

      数组列(ArrayList)与数组类似,但在给它增加元素时,能够自动扩展,而数组则有固定大小,其属于System.Collections命名空间的一部分。

它有Add,AddRange,Insert,InsertRange,Sort等方法,可理解为动态数组。例如:

/*
  Example11_2.cs illustrates the use of ArrayList properties
  and methods
*/

using System;
using System.Collections;

class Example11_2
{

  // the DisplayArrayList() method displays the elements in the
  // ArrayList that is supplied as a parameter
  public static void DisplayArrayList(
    string arrayListName, ArrayList myArrayList
  )
  {
    for (int counter = 0; counter < myArrayList.Count; counter++)
    {
      Console.WriteLine(arrayListName + "[" + counter + "] = " +
        myArrayList[counter]);
    }
  }

  public static void Main()
  {

    // create an ArrayList object
    ArrayList myArrayList = new ArrayList();

    // set and display the Capacity property
    myArrayList.Capacity = 10;
    Console.WriteLine("myArrayList.Capacity = " +
      myArrayList.Capacity);

    // display the IsFixedSize and IsReadOnly properties
    Console.WriteLine("myArrayList.IsFixedSize = " +
      myArrayList.IsFixedSize);
    Console.WriteLine("myArrayList.IsReadOnly = " +
      myArrayList.IsReadOnly);

    // add eight string elements to myArrayList using the Add(),
    // Insert(), AddRange(), and InsertRange() methods
    Console.WriteLine("Adding eight string elements to myArrayList");
    myArrayList.Add("This");
    myArrayList.Insert(1, "is");
    string[] myStringArray = {"a", "test"};
    myArrayList.AddRange(myStringArray);
    string[] anotherStringArray = {"Here's", "some", "more", "text"};
    myArrayList.InsertRange(myArrayList.Count, anotherStringArray);

    // display the elements in myArrayList using the
    // DisplayArrayList() method defined earlier
    DisplayArrayList("myArrayList", myArrayList);

    // use the SetRange() method to copy the elements from
    // anotherStringArray to myArrayList, starting at index 0
    Console.WriteLine("Using the SetRange() method to copy the\n" +
      "elements from anotherStringArray to myArrayList,\n" +
      "starting at index 0");
    myArrayList.SetRange(0, anotherStringArray);
    DisplayArrayList("myArrayList", myArrayList);

    // use the Contains() method to determine if the string "text"
    // is in the ArrayList; if it is, then use the IndexOf() and
    // LastIndexOf() methods to display the first and last occurrence
    if (myArrayList.Contains("text"))
    {
      int index = myArrayList.IndexOf("text");
      Console.WriteLine("myArrayList does contain the word 'text'");
      Console.WriteLine("'text' first occurs at index " + index);
      index = myArrayList.LastIndexOf("text");
      Console.WriteLine("'text' last occurs at index " + index);
    }

    // remove element 0, first "text" element, and two
    // elements starting at index 3
    Console.WriteLine("Removing elements from myArrayList");
    myArrayList.RemoveAt(0);
    myArrayList.Remove("text");
    myArrayList.RemoveRange(3, 2);
    DisplayArrayList("myArrayList", myArrayList);

    // use the Sort() method to sort myArrayList
    Console.WriteLine("Using the Sort() method to sort myArrayList");
    myArrayList.Sort();
    DisplayArrayList("myArrayList", myArrayList);

    // use the BinarySearch() method to search myArrayList
    Console.WriteLine("Using the BinarySearch() method to search myArrayList");
    int index2 = myArrayList.BinarySearch("some");
    Console.WriteLine("Found 'some' at index " + index2);

    // use the Reverse() method to reverse myArrayList
    Console.WriteLine("Using the Reverse() method");
    myArrayList.Reverse();
    DisplayArrayList("myArrayList", myArrayList);

    // use the TrimToSize() method to reduce the capacity of
    // myArrayList to the actual number of elements in myArrayList
    Console.WriteLine("Using the TrimToSize() method to reduce the\n" +
      "capacity of myArrayList");
    myArrayList.TrimToSize();
    Console.WriteLine("myArrayList.Capacity = " +
      myArrayList.Capacity);

    // use the GetRange() method to get a range of elements
    // from myArrayList
    Console.WriteLine("Using the GetRange() method to get two\n" +
      "elements from myArrayList, starting at index 1");
    ArrayList anotherArrayList = myArrayList.GetRange(1, 2);
    DisplayArrayList("anotherArrayList", anotherArrayList);

    // get an enumerator using the GetEnumerator() method
    // and use it to read the elements in myArrayList
    Console.WriteLine("Using the GetEnumerator() method to get an enumerator");
    IEnumerator myEnumerator = myArrayList.GetEnumerator();
    while (myEnumerator.MoveNext())
    {
      Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    }

    // use the Reset() method and access the first row again using MoveNext()
    Console.WriteLine("Using the Reset() method and accessing\n" +
      " the first row again using MoveNext()");
    myEnumerator.Reset();
    myEnumerator.MoveNext();
    Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);

    // Use a foreach statement to read the contents of myArrayList
    Console.WriteLine("Using a foreach statement to read the contents of myArrayList");
    foreach (string myString in myArrayList)
    {
      System.Console.WriteLine("myString = " + myString);
    }

  }

}
相关文章
|
3月前
|
机器学习/深度学习 数据可视化 API
Python Numpy 切片和索引(高级索引、布尔索引、花式索引)
Python Numpy 切片和索引(高级索引、布尔索引、花式索引)
44 3
|
4月前
|
存储 语音技术 索引
语音识别,列表的定义语法,列表[],列表的下标索引,从列表中取出来特定的数据,name[0]就是索引,反向索引,头部是-1,my[1][1],嵌套列表使用, 列表常用操作, 函数一样,需引入
语音识别,列表的定义语法,列表[],列表的下标索引,从列表中取出来特定的数据,name[0]就是索引,反向索引,头部是-1,my[1][1],嵌套列表使用, 列表常用操作, 函数一样,需引入
|
算法 前端开发
前端算法-Excel表的列名称
前端算法-Excel表的列名称
|
关系型数据库 MySQL 数据库
MySql基础-笔记6 -排序、分组、连接的使用、NULL值处理
MySql基础-笔记6 -排序、分组、连接的使用、NULL值处理
195 0
MySql基础-笔记6 -排序、分组、连接的使用、NULL值处理
|
索引 Python
python 对一组list数据,进行区间划分,按照大小排序并返回索引值
最近在对海洋数据进行处理时,对数据需要进行一些排序,数据匹配等操作; 现对我所希望实现的一些函数进行总结:
python 对一组list数据,进行区间划分,按照大小排序并返回索引值
|
Java 数据挖掘 索引
【python数据分析】数据索引的创建,取值,排序
文章目录 索引的创建,取值,排序 1.多层索引的创建 2.多层索引的取值
【python数据分析】数据索引的创建,取值,排序
|
存储 NoSQL 搜索推荐
索引的概述和类型 | 学习笔记
快速学习 索引的概述和类型
索引的概述和类型 | 学习笔记
|
JSON 分布式计算 Hadoop
创建索引库和索引演示 | 学习笔记
快速学习创建索引库和索引演示
创建索引库和索引演示 | 学习笔记
|
索引
十五、避免在索引列上使用内置函数
十五、避免在索引列上使用内置函数
85 0
|
存储 机器学习/深度学习 缓存
FaissPQ索引简介
FaissPQ索引简介
264 0
FaissPQ索引简介
下一篇
无影云桌面