zoukankan      html  css  js  c++  java
  • Array用法详解

    1  Array

    (1) 提供创建、操作、搜索和排序数组的方法,因而在公共语言运行库中用作所有数组的基类。

    (2)public abstract class Array : ICloneable, IList, ICollection, IEnumerable

    (3)Array 类是支持数组的语言实现的基类。但是,只有系统和编译器能够从 Array 类显式派生。用户应当使用由语言提供的数组构造。

    一个元素就是 Array 中的一个值。Array 的长度是它可包含的元素总数。Array 的秩是 Array 中的维数。Array 中维度的下限是 Array 中该维度的起始索引,多维 Array 的各个维度可以有不同的界限。

    (4)重要事项:在 .NET Framework 2.0 版中,Array 类实现 System.Collections.Generic.IListSystem.Collections.Generic.ICollectionSystem.Collections.Generic.IEnumerable 泛型接口。由于实现是在运行时提供给数组的,因而对于文档生成工具不可见。因此,泛型接口不会出现在 Array 类的声明语法中,也不会有关于只能通过将数组强制转换为泛型接口类型(显式接口实现)才可访问的接口成员的参考主题。将某一数组强制转换为这三种接口之一时需要注意的关键一点是,添加、插入或移除元素的成员会引发 NotSupportedException

    (5)Type 对象提供有关数组类型声明的信息。具有相同数组类型的 Array 对象共享同一 Type 对象。

    (6)Type.IsArrayType.GetElementType 可能不返回所预期的 Array 形式的结果,因为如果某个数组被强制转换为 Array 类型,则结果是对象,而非数组。即,typeof(System.Array).IsArray 返回 false,而 typeof(System.Array).GetElementType 返回 空引用(在 Visual Basic 中为 Nothing)。

    (7)与大多数类不同,Array 提供 CreateInstance 方法,以便允许后期绑定访问,而不是提供公共构造函数。

    (8)Array.Copy 方法不仅可在同一类型的数组之间复制元素,而且可在不同类型的标准数组之间复制元素;它会自动处理强制类型转换。

    (9)有些方法,如 CreateInstanceCopyCopyToGetValueSetValue,提供重载(接受 64 位整数作为参数),以适应大容量数组。LongLengthGetLongLength 返回指示数组长度的 64 位整数。

    (10)不保证会对 Array 进行排序。在执行需要对 Array 进行排序的操作(如 BinarySearch)之前,必须对 Array 进行排序。

    2   下面的代码示例说明 Array.Copy 如何在 integer 类型的数组和 Object 类型的数组之间复制元素。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.IO;
    
    namespace Array
    {
       
        class Program
        {
            public static void PrintValues(Object[] myArr) //打印对象数组            
            {
                foreach (Object i in myArr)
                {
                    Console.Write("\t{0}", i);
                }
                Console.WriteLine();
            }
    
            public static void PrintValues(int[] myArr) //打印整形数组
            {
                foreach (int i in myArr)
                {
                    Console.Write("\t{0}", i);
                }
                Console.WriteLine();           
            }       
    
            static void Main(string[] args)
            {
    
                // Creates and initializes a new integer array and a new Object array.
                int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
                Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 };
    
                // Prints the initial values of both arrays.
                Console.WriteLine("Initially,");
                Console.Write("integer array:");
                PrintValues(myIntArray);
                
                Console.Write("Object array: ");
                PrintValues(myObjArray);
    
                // Copies the first two elements from the integer array to the Object array.
                System.Array.Copy(myIntArray, myObjArray, 2);
    
                // Prints the values of the modified arrays.
                Console.WriteLine("\nAfter copying the first two elements of the integer array to the Object array,");
                Console.Write("integer array:");
                PrintValues(myIntArray);
                Console.Write("Object array: ");
                PrintValues(myObjArray);
    
                // Copies the last two elements from the Object array to the integer array.
                System.Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2);
                //该数组是一维的,所以用GetUpperBound(0)得到第0维的上限
    
                // Prints the values of the modified arrays.
                Console.WriteLine("\nAfter copying the last two elements of the Object array to the integer array,");
                Console.Write("integer array:");
                PrintValues(myIntArray);
                Console.Write("Object array: ");
                PrintValues(myObjArray);
    
                Console.Read();
            }  
           
        }
    }

    3 下面的代码示例创建并初始化一个 Array,然后显示其属性和元素。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.IO;

    namespace Sample
    {
       
        class Program
        {
            public static void PrintValues(Array myArr)
            {
                System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
                int i = 0;//用来控制换行
                int cols = myArr.GetLength(myArr.Rank - 1);
                //myRank是3维,GetLength是从第0维开始的           

                while (myEnumerator.MoveNext())
                {
                    if (i < cols)
                    {
                        i++;
                    }
                    else
                    {
                        Console.WriteLine();
                        i = 1;
                    }
                    Console.Write("\t{0}", myEnumerator.Current);
                }
                Console.WriteLine();
            }      

            static void Main(string[] args)
            {
                // Creates and initializes a new three-dimensional Array of type Int32.
                Array myArr = Array.CreateInstance(typeof(Int32), 2, 3, 4);
                for (int i = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++)
                    for (int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++)
                        for (int k = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++)
                        {
                            myArr.SetValue((i * 100) + (j * 10) + k, i, j, k);
                        }

                // Displays the properties of the Array.
                Console.WriteLine("The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length);
                Console.WriteLine("\tLength\tLower\tUpper");
                for (int i = 0; i < myArr.Rank; i++)
                {
                    Console.Write("{0}:\t{1}", i, myArr.GetLength(i));
                    Console.WriteLine("\t{0}\t{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i));
                }

                // Displays the contents of the Array.
                Console.WriteLine("The Array contains the following values:");
                PrintValues(myArr);   

                Console.Read();
            }  
           
        }
    }

     

  • 相关阅读:
    Atitit.Java exe bat  作为windows系统服务程序运行
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit. Object-c语言 的新的特性  attilax总结
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit。Time base gc 垃圾 资源 收集的原理与设计
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.go语言golang语言的新的特性  attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.pdf 预览 转换html attilax总结
    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结
  • 原文地址:https://www.cnblogs.com/wang7/p/2508364.html
Copyright © 2011-2022 走看看