C#方法Array.Reverse、CreateInstance、SetValue、GetValue、GetLowerBound(Int32) 、GetUpperBound(Int32)

发布时间:2024年01月09日

目录

一、重载

二、Reverse(Array, Int32, Int32)

1.定义

2.实例

三、?Reverse(Array)

1.定义

2.实例

四、Reverse(T[])

五、Reverse(T[], Int32, Int32)

六、实例中的知识点

1.Array.CreateInstance 方法

(1)重载

(2)CreateInstance(Type, Int32)

(3)CreateInstance(Type, Int32[])

(4)CreateInstance(Type, Int64[])

(5)CreateInstance(Type, Int32, Int32)

(6)CreateInstance(Type, Int32[], Int32[])

(7)CreateInstance(Type, Int32, Int32, Int32)

2.Array.SetValue 方法

(1)重载

(2)SetValue(Object, Int32)

(3)SetValue(Object, Int32[])

(4)SetValue(Object, Int64)

(5)?SetValue(Object, Int64[])

(6)SetValue(Object, Int32, Int32)

(7)SetValue(Object, Int64, Int64)

(8)SetValue(Object, Int32, Int32, Int32)

(9)SetValue(Object, Int64, Int64, Int64)

(10)示例

3.Array.GetValue 方法

(1)重载

(2)GetValue(Int32)

(3)GetValue(Int32[])

(4)GetValue(Int64)

(5)GetValue(Int64[])

(6)GetValue(Int32, Int32)

(7)GetValue(Int64, Int64)

(8)GetValue(Int32, Int32, Int32)

(9)GetValue(Int64, Int64, Int64)

4.Array.GetLowerBound(Int32) 方法

(1)定义

(2)示例

5.Array.GetUpperBound(Int32) 方法


????????命名空间:
????????System
????????程序集:
????????System.Runtime.dll

????????反转一维 Array 或部分 Array 中元素的顺序。

一、重载

Reverse(Array, Int32, Int32)反转一维?Array?中元素子集的顺序。
Reverse(Array)反转整个一维?Array?中元素的顺序。
Reverse<T>(T[])反转一维泛型数组中元素的顺序。
Reverse<T>(T[], Int32, Int32)反转一维泛型数组中元素子集的顺序。

二、Reverse(Array, Int32, Int32)

????????反转一维 Array 中元素子集的顺序。

1.定义

public static void Reverse (Array array, int index, int length);

参数
array
Array
要反转的一维 Array。

index
Int32
要反转的部分的起始索引。

length
Int32
要反转的部分中的元素数。

例外
ArgumentNullException
array 为 null。

RankException
array 是多维的。

ArgumentOutOfRangeException
index 小于 array 的下限。
或 -
length 小于零。

ArgumentException
index 和 length 未在 array 中指定有效范围。

2.实例

????????如何在一 Array系列元素中反转值的排序。

// Array.Reverse 方法
namespace Reverse
{
    public class SamplesArray1
    {
        public static void Main()
        {
            // Creates and initializes a new Array.
            Array myArray = Array.CreateInstance(typeof(string), 9);
            myArray.SetValue("The", 0);
            myArray.SetValue("QUICK", 1);
            myArray.SetValue("BROWN", 2);
            myArray.SetValue("FOX", 3);
            myArray.SetValue("jumps", 4);
            myArray.SetValue("over", 5);
            myArray.SetValue("the", 6);
            myArray.SetValue("lazy", 7);
            myArray.SetValue("dog", 8);

            Console.WriteLine("The Array initially contains the following values:");
            PrintIndexAndValues(myArray);
            Array.Reverse(myArray, 1, 3);   //索引1开始的3个数组元素颠倒顺序
            Console.WriteLine("After reversing:");
            PrintIndexAndValues(myArray);
        }

        public static void PrintIndexAndValues(Array myArray)
        {
            for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
                Console.WriteLine("\t[{0}]:\t{1}", i, myArray.GetValue(i));
        }
    }
}
/*
运行结果:
The Array initially contains the following values:
        [0]:    The
        [1]:    QUICK
        [2]:    BROWN
        [3]:    FOX
        [4]:    jumps
        [5]:    over
        [6]:    the
        [7]:    lazy
        [8]:    dog
After reversing:
        [0]:    The
        [1]:    FOX
        [2]:    BROWN
        [3]:    QUICK
        [4]:    jumps
        [5]:    over
        [6]:    the
        [7]:    lazy
        [8]:    dog

*/

三、?Reverse(Array)

????????反转整个一维 Array 中元素的顺序。

1.定义

public static void Reverse (Array array);

参数
array
Array
要反转的一维 Array。

例外
ArgumentNullException
array 为 null。

RankException
array 是多维的。

2.实例

?????????反转一个 Array值中的值排序。

// Reverse(Array)
namespace Reverse1
{
    public class SamplesArray
    {
        public static void Main()
        {
            // Creates and initializes a new Array.
            Array myArray = Array.CreateInstance(typeof(string), 9);
            myArray.SetValue("The", 0);
            myArray.SetValue("quick", 1);
            myArray.SetValue("brown", 2);
            myArray.SetValue("fox", 3);
            myArray.SetValue("jumps", 4);
            myArray.SetValue("over", 5);
            myArray.SetValue("the", 6);
            myArray.SetValue("lazy", 7);
            myArray.SetValue("dog", 8);

            Console.WriteLine("The Array initially contains the following values:");
            PrintIndexAndValues(myArray);
            Array.Reverse(myArray); //颠倒数组中所有元素的顺序
            Console.WriteLine("After reversing:");
            PrintIndexAndValues(myArray);
        }

        public static void PrintIndexAndValues(Array myArray)
        {
            for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
                Console.WriteLine("\t[{0}]:\t{1}", i, myArray.GetValue(i));
        }
    }
}
/*
运行结果:
The Array initially contains the following values:
        [0]:    The
        [1]:    quick
        [2]:    brown
        [3]:    fox
        [4]:    jumps
        [5]:    over
        [6]:    the
        [7]:    lazy
        [8]:    dog
After reversing:
        [0]:    dog
        [1]:    lazy
        [2]:    the
        [3]:    over
        [4]:    jumps
        [5]:    fox
        [6]:    brown
        [7]:    quick
        [8]:    The

*/

四、Reverse<T>(T[])

????????反转一维泛型数组中元素的顺序。

public static void Reverse<T> (T[] array);

类型参数
T
array 中的元素的类型。

参数
array
T[]
要反转的元素的一维数组。

例外
ArgumentNullException
array 为 null。

RankException
array 是多维的。

五、Reverse<T>(T[], Int32, Int32)

????????反转一维泛型数组中元素子集的顺序

public static void Reverse<T> (T[] array, int index, int length);

类型参数
T
array 中的元素的类型。

参数
array
T[]
要反转的元素的一维数组。

index
Int32
要反转的部分的起始索引。

length
Int32
要反转的部分中的元素数。

例外
ArgumentNullException
array 为 null。

RankException
array 是多维的。

ArgumentOutOfRangeException
index 小于 array 的下限。
或 -
length 小于零。

ArgumentException
index 和 length 未在 array 中指定有效范围。

六、实例中的知识点

1.Array.CreateInstance 方法

????????初始化 Array 类的新实例。

(1)重载

CreateInstance(Type, Int32)创建使用从零开始的索引、具有指定?Type?和长度的一维?Array
CreateInstance(Type, Int32[])创建索引从零开始、具有指定?Type?和维长的多维?Array?维的长度在一个?32?位整数数组中指定。
CreateInstance(Type, Int64[])创建索引从零开始、具有指定?Type?和维长的多维?Array?维度的长度在一个?64?位整数数组中指定。
CreateInstance(Type, Int32, Int32)创建使用从零开始的索引、具有指定?Type?和维长的二维?Array
CreateInstance(Type, Int32[], Int32[])创建具有指定下限、指定?Type?和维长的多维?Array
CreateInstance(Type, Int32, Int32, Int32)创建使用从零开始的索引、具有指定?Type?和维长的三维?Array

(2)CreateInstance(Type, Int32)

?????????创建使用从零开始的索引、具有指定 Type 和长度的一维 Array。

public static Array CreateInstance (Type elementType, int length);

参数
elementType
Type
要创建的 Array 的 Type。

length
Int32
要创建的 Array 的大小。

返回
Array
使用从零开始的索引、具有指定 Type 和长度的新的一维 Array。

例外
ArgumentNullException
elementType 为 null。

ArgumentException
elementType 不是有效的 Type。

NotSupportedException
不支持 elementType。 例如,不支持 Void。
- 或 -
elementType 为开放式泛型类型。

ArgumentOutOfRangeException
length 小于零。

????????示例

// CreateInstance(Type, Int32)
// 创建和初始化一维 Array。
using System.Collections;
namespace ConsoleApp3
{
    public class SamplesArray
    {
        public static void Main()
        {
            // 创建和初始化数组,数组type int.
            Array my1DArray = Array.CreateInstance(typeof(int), 5);
            for (int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++)
                my1DArray.SetValue(i + 10, i);
            // 输出数组各元素
            Console.WriteLine("The one-dimensional Array contains the following values:");
            for (int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++)
                Console.Write("\t{0}", my1DArray.GetValue(i));
            Console.WriteLine();
        }
    }
}
/*
运行结果:
The one-dimensional Array contains the following values:
        10      11      12      13      14

*/

(3)CreateInstance(Type, Int32[])

????????创建索引从零开始、具有指定 Type 和维度的多维 Array。 数组维度在一个 32 位整数数组中指定。

public static Array CreateInstance (Type elementType, params int[] lengths);

参数
elementType
Type
要创建的 Array 的 Type。

lengths
Int32[]
一个 32 位整数数组,它表示要创建的 Array 中每个维度的大小。

返回
Array
指定 Type 的新多维 Array,该数组每个维具有指定长度,且使用从零开始的索引。

例外
ArgumentNullException
elementType 为 null。
或
lengths 为 null。

ArgumentException
elementType 不是有效的 Type。
- 或 -
lengths 数组包含的元素少于一个。

NotSupportedException
不支持 elementType。 例如,不支持 Void。
- 或 -
elementType 为开放式泛型类型。

ArgumentOutOfRangeException
lengths 中的任何值都小于零。

????????示例

// CreateInstance(Type, Int32[])
// 创建和初始化多维 Array
namespace ConsoleApp4
{
    public class SamplesArray3
    {
        public static void Main()
        {
            // 创建和初始化一个多维数组,数组type string.
            int[] myLengthsArray = [2, 3, 4, 5];
            Array my4DArray = Array.CreateInstance(typeof(string), myLengthsArray);
            for (int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++)
                for (int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++)
                    for (int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++)
                        for (int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++)
                        {
                            int[] myIndicesArray = [i, j, k, l];
                            my4DArray.SetValue(Convert.ToString(i)+ j + k + l, myIndicesArray);
                        }

            //输出多维数组各元素
            Console.WriteLine("The four-dimensional Array contains the following values:");
            for (int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++)
                for (int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++)
                    for (int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++)
                    {
                        for (int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++)
                        {
                            int[] myIndicesArray = [i, j, k, l];
                            Console.Write("{0}\t", my4DArray.GetValue(myIndicesArray));
                        }
                        Console.WriteLine();
                    }                       
        }  
    }
}
/*
运行结果:
The four-dimensional Array contains the following values:
0000    0001    0002    0003    0004
0010    0011    0012    0013    0014
0020    0021    0022    0023    0024
0030    0031    0032    0033    0034
0100    0101    0102    0103    0104
0110    0111    0112    0113    0114
0120    0121    0122    0123    0124
0130    0131    0132    0133    0134
0200    0201    0202    0203    0204
0210    0211    0212    0213    0214
0220    0221    0222    0223    0224
0230    0231    0232    0233    0234
1000    1001    1002    1003    1004
1010    1011    1012    1013    1014
1020    1021    1022    1023    1024
1030    1031    1032    1033    1034
1100    1101    1102    1103    1104
1110    1111    1112    1113    1114
1120    1121    1122    1123    1124
1130    1131    1132    1133    1134
1200    1201    1202    1203    1204
1210    1211    1212    1213    1214
1220    1221    1222    1223    1224
1230    1231    1232    1233    1234

*/

(4)CreateInstance(Type, Int64[])

????????创建索引从零开始、具有指定 Type 和维长的多维 Array。 维度的长度在一个 64 位整数数组中指定。

public static Array CreateInstance (Type elementType, params long[] lengths);

参数
elementType
Type
要创建的 Array 的 Type。

lengths
Int64[]
一个 64 位整数数组,它表示要创建的 Array 中每个维度的大小。 数组中的每个整数都必须介于零和 Int32.MaxValue 之间(含)。

返回
Array
指定 Type 的新多维 Array,该数组每个维具有指定长度,且使用从零开始的索引。

例外
ArgumentNullException
elementType 为 null。
或
lengths 为 null。

ArgumentException
elementType 不是有效的 Type。
- 或 -
lengths 数组包含的元素少于一个。

NotSupportedException
不支持 elementType。 例如,不支持 Void。
- 或 -
elementType 为开放式泛型类型。

ArgumentOutOfRangeException
中的任何 lengths 值都小于零或大于 Int32.MaxValue。

(5)CreateInstance(Type, Int32, Int32)

?????????创建使用从零开始的索引、具有指定 Type 和维长的二维 Array。

public static Array CreateInstance (Type elementType, int length1, int length2);

参数
elementType
Type
要创建的 Array 的 Type。

length1
Int32
要创建的 Array 的第一维的大小。

length2
Int32
要创建的 Array 的第二维的大小。

返回
Array
使用从零开始的索引、具有指定 Type 的新的二维 Array,其每个维度都为指定的长度。

例外
ArgumentNullException
elementType 为 null。

ArgumentException
elementType 不是有效的 Type。

NotSupportedException
不支持 elementType。 例如,不支持 Void。
- 或 -
elementType 为开放式泛型类型。

ArgumentOutOfRangeException
length1 小于零。
或
length2 小于零。

?????????示例

// CreateInstance(Type, Int32, Int32)
// 创建和初始化二维 Array
namespace ConsoleApp5
{
    public class SamplesArray1
    {
        public static void Main()
        {
            // 创建和初始化一个二维数组,数组type string.
            Array my2DArray = Array.CreateInstance(typeof(string), 2, 3);
            for (int i = my2DArray.GetLowerBound(0); i <= my2DArray.GetUpperBound(0); i++)
                for (int j = my2DArray.GetLowerBound(1); j <= my2DArray.GetUpperBound(1); j++)
                    my2DArray.SetValue("abc" + i + j, i, j);    //字符串连接的方法,隐形转换

            // 显示数组各元素
            Console.WriteLine("The two-dimensional Array contains the following values:");
            //PrintValues(my2DArray);
            for (int i = my2DArray.GetLowerBound(0); i <= my2DArray.GetUpperBound(0); i++)
            {
                for (int j = my2DArray.GetLowerBound(1); j <= my2DArray.GetUpperBound(1); j++)
                    Console.Write("{0}\t", my2DArray.GetValue(i, j));
                Console.WriteLine();
            }
        }
    }
}
/*
运行结果:
The two-dimensional Array contains the following values:
abc00   abc01   abc02
abc10   abc11   abc12

*/

(6)CreateInstance(Type, Int32[], Int32[])

????????创建具有指定下限、指定 Type 和维长的多维 Array。

public static Array CreateInstance (Type elementType, int[] lengths, int[] lowerBounds);

参数
elementType
Type
要创建的 Array 的 Type。

lengths
Int32[]
一维数组,它包含要创建的 Array 的每个维度的大小。

lowerBounds
Int32[]
一维数组,它包含要创建的 Array 的每个维度的下限(起始索引)。

返回
Array
具有指定 Type 的新的多维 Array,其每个维度都具有指定长度和下限。

例外
ArgumentNullException
elementType 为 null。
或
lengths 上声明的默认值为 null。
或
lowerBounds 为 null。

ArgumentException
elementType 不是有效的 Type。
- 或 -
lengths 数组包含的元素少于一个。
- 或 -
lengths 和 lowerBounds 数组包含的元素数不同。

NotSupportedException
不支持 elementType。 例如,不支持 Void。
- 或 -
elementType 为开放式泛型类型。

ArgumentOutOfRangeException
lengths 中的任何值都小于零。
- 或 -
方法中的任何 lowerBounds 值都非常大,因此维度的下限和长度之和大于 Int32.MaxValue。

?????????示例

// CreateInstance(Type, Int32[], Int32[])
// 创建和初始化具有指定下限的多维 Array
namespace ConsoleApp7
{
    public class SamplesArray4
    {
        public static void Main()
        {
            // 创建和初始化一个多维数组,数组type string.
            int[] myLengthsArray = [3, 5];  //约束新建数组每个维度的长度
            int[] myBoundsArray = [2, 3];   //约束新建数组每个维度的下限
            Array myArray = Array.CreateInstance(typeof(string), myLengthsArray, myBoundsArray);
            for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
                for (int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++)
                {
                    int[] myIndicesArray = [i, j];
                    myArray.SetValue(Convert.ToString(i) + j, myIndicesArray);
                }

            // Displays the lower bounds and the upper bounds of each dimension.
            Console.WriteLine("Bounds:\tLower\tUpper");
            for (int i = 0; i < myArray.Rank; i++)  // myArray.Rank数组的维数=2
                Console.WriteLine("维数{0}:\t下限{1}\t上限{2}", i, myArray.GetLowerBound(i), myArray.GetUpperBound(i));

            // Displays the values of the Array.
            Console.WriteLine("The Array contains the following values:");
            for (int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++)
            {
                for (int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++)
                {
                    int[] myIndicesArray = [i, j];
                    Console.Write("{0}\t", myArray.GetValue(myIndicesArray));
                }
                Console.WriteLine();
            }             
        }       
    }
}
/*
运行结果:
Bounds: Lower   Upper
维数0:  下限2   上限4
维数1:  下限3   上限7
The Array contains the following values:
23      24      25      26      27
33      34      35      36      37
43      44      45      46      47

*/

(7)CreateInstance(Type, Int32, Int32, Int32)

????????创建使用从零开始的索引、具有指定 Type 和维长的三维 Array。

public static Array CreateInstance (Type elementType, int length1, int length2, int length3);

参数
elementType
Type
要创建的 Array 的 Type。

length1
Int32
要创建的 Array 的第一维的大小。

length2
Int32
要创建的 Array 的第二维的大小。

length3
Int32
要创建的 Array 的第三维的大小。

返回
Array
每个维具有指定长度、使用从零开始的索引的指定 Type 的新三维 Array。

例外
ArgumentNullException
elementType 为 null。

ArgumentException
elementType 不是有效的 Type。

NotSupportedException
不支持 elementType。 例如,不支持 Void。
- 或 -
elementType 为开放式泛型类型。

ArgumentOutOfRangeException
length1 小于零。
或
length2 小于零。
或
length3 小于零。

?????????示例

// CreateInstance(Type, Int32, Int32, Int32)
// 创建和初始化三维 Array
namespace ConsoleApp6
{
    public class SamplesArray2
    {
        public static void Main()
        {
            // 创建和初始化一个三维数组,数组type Object.
            Array my3DArray = Array.CreateInstance(typeof(object), 2, 3, 4);
            for (int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++)
                for (int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++)
                    for (int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++)
                        my3DArray.SetValue("abc" + i + j + k, i, j, k);

            // 输出数组个元素的值
            Console.WriteLine("The three-dimensional Array contains the following values:");
            for (int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++)
                for (int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++)
                {
                    for (int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++)
                        Console.Write("{0}\t", my3DArray.GetValue(i, j, k));
                    Console.WriteLine();
                }                   
        }  
    }
}
/*
运行结果:
The three-dimensional Array contains the following values:
abc000  abc001  abc002  abc003
abc010  abc011  abc012  abc013
abc020  abc021  abc022  abc023
abc100  abc101  abc102  abc103
abc110  abc111  abc112  abc113
abc120  abc121  abc122  abc123

*/

2.Array.SetValue 方法

????????将当前 Array 中的指定元素设置为指定值。

(1)重载

SetValue(Object, Int32)将值设置为一维?Array?中指定位置的元素。?索引指定为?32?位整数。
SetValue(Object, Int32[])将值设置为多维?Array?中指定位置的元素。?索引指定为一个?32?位整数数组。
SetValue(Object, Int64)将值设置为一维?Array?中指定位置的元素。?索引指定为?64?位整数。
SetValue(Object, Int64[])将值设置为多维?Array?中指定位置的元素。?索引指定为一个?64?位整数数组。
SetValue(Object, Int32, Int32)将某值设置给二维?Array?中指定位置的元素。?索引指定为?32?位整数。
SetValue(Object, Int64, Int64)将某值设置给二维?Array?中指定位置的元素。?索引指定为?64?位整数。
SetValue(Object, Int32, Int32, Int32)将值设置为三维?Array?中指定位置的元素。?索引指定为?32?位整数。
SetValue(Object, Int64, Int64, Int64)将值设置为三维?Array?中指定位置的元素。?索引指定为?64?位整数。

(2)SetValue(Object, Int32)

?????????将值设置为一维 Array 中指定位置的元素。 索引指定为 32 位整数。

public void SetValue (object value, int index);

参数
value
Object
指定元素的新值。

index
Int32
一个 32 位整数,它表示要设置的 Array 元素的位置。

例外
ArgumentException
当前 Array 不是正好具有一个维度。

InvalidCastException
value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException
index 超出了当前 Array 的有效索引的范围。

注解
和GetLowerBoundGetUpperBound方法可以确定值index是否超出边界。

(3)SetValue(Object, Int32[])

?????????将值设置为多维 Array 中指定位置的元素。 索引指定为一个 32 位整数数组。

public void SetValue (object value, params int[] indices);

参数
value
Object
指定元素的新值。

indices
Int32[]
32 位整数的一维数组,它表示用于指定要设置的元素的位置的索引。

例外
ArgumentNullException
indices 为 null。

ArgumentException
当前 Array 中的维数不等于 indices 中的元素数。

InvalidCastException
value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException
indices 中的任何元素都超出了当前 Array 的相应维度的有效索引范围。

注解
方法中的indices元素数必须等于.Array中的维度数。 数组中的所有indices元素必须统一指定多维Array元素中所需元素的位置。
和GetLowerBoundGetUpperBound方法可以确定数组中的任何indices值是否超出边界。

(4)SetValue(Object, Int64)

?????????将值设置为一维 Array 中指定位置的元素。 索引指定为 64 位整数。

public void SetValue (object value, long index);

参数
value
Object
指定元素的新值。

index
Int64
一个 64 位整数,它表示要设置的 Array 元素的位置。

例外
ArgumentException
当前 Array 不是正好具有一个维度。

InvalidCastException
value 不能转换为当前 Array 的元素类型。

ArgumentOutOfRangeException
index 超出了当前 Array 的有效索引的范围。

注解
和GetLowerBoundGetUpperBound方法可以确定值index是否超出边界。

(5)?SetValue(Object, Int64[])

????????将值设置为多维 Array 中指定位置的元素。 索引指定为一个 64 位整数数组。

public void SetValue (object value, params long[] indices);

参数
value
Object
指定元素的新值。

indices
Int64[]
64位整数的一维数组,它表示用于指定要设置元素的位置索引。

例外
ArgumentNullException
indices 为 null。

ArgumentException
当前Array中的维数不等于indices中的元素数。

InvalidCastException
value不能转换为当前Array的元素类型。

ArgumentOutOfRangeException
indices中的任何元素都超出了当前Array的相应维度的有效索引范围。

注解
方法中的indices元素数必须等于.Array中的维度数。 数组中的所有indices元素必须统一指定多维Array元素中所需元素的位置。
和GetLowerBoundGetUpperBound方法可以确定数组中的任何indices值是否超出边界。

(6)SetValue(Object, Int32, Int32)

????????将某值设置给二维 Array 中指定位置的元素。 索引指定为 32 位整数。

public void SetValue (object value, int index1, int index2);

参数
value
Object
指定元素的新值。

index1
Int32
一个 32 位整数,它表示要设置的 Array 元素的第一维索引。

index2
Int32
一个 32 位整数,它表示要设置的 Array 元素的第二维索引。

例外
ArgumentException
当前 Array 不是正好具有两个维度。

InvalidCastException
value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException
index1 或 index2 超出了当前 Array 的相应维度的有效索引范围。

注解
这些 GetLowerBound 索引和 GetUpperBound 方法可以确定任何索引是否超出边界。

(7)SetValue(Object, Int64, Int64)

?????????将某值设置给二维 Array 中指定位置的元素。 索引指定为 64 位整数。

public void SetValue (object value, long index1, long index2);

参数
value
Object
指定元素的新值。

index1
Int64
一个 64 位整数,它表示要设置的 Array 元素的第一维索引。

index2
Int64
一个 64 位整数,它表示要设置的 Array 元素的第二维索引。

例外
ArgumentException
当前 Array 不是正好具有两个维度。

InvalidCastException
value 不能转换为当前 Array 的元素类型。

ArgumentOutOfRangeException
index1 或 index2 超出了当前 Array 的相应维度的有效索引范围。

注解
这些 GetLowerBound 索引和 GetUpperBound 方法可以确定任何索引是否超出边界。

(8)SetValue(Object, Int32, Int32, Int32)

?????????将值设置为三维 Array 中指定位置的元素。 索引指定为 32 位整数。

public void SetValue (object value, int index1, int index2, int index3);

参数
value
Object
指定元素的新值。

index1
Int32
一个 32 位整数,它表示要设置的 Array 元素的第一维索引。

index2
Int32
一个 32 位整数,它表示要设置的 Array 元素的第二维索引。

index3
Int32
一个 32 位整数,它表示要设置的 Array 元素的第三维索引。

例外
ArgumentException
当前 Array 不是正好具有三个维度。

InvalidCastException
value 不能转换为当前 Array 的元素类型。

IndexOutOfRangeException
index1 或 index2 或 index3 超出了当前 Array 的相应维度的有效索引范围。

注解
这些 GetLowerBound 索引和 GetUpperBound 方法可以确定任何索引是否超出边界。

(9)SetValue(Object, Int64, Int64, Int64)

?????????将值设置为三维 Array 中指定位置的元素。 索引指定为 64 位整数。

public void SetValue (object value, long index1, long index2, long index3);

参数
value
Object
指定元素的新值。

index1
Int64
一个 64 位整数,它表示要设置的 Array 元素的第一维索引。

index2
Int64
一个 64 位整数,它表示要设置的 Array 元素的第二维索引。

index3
Int64
一个 64 位整数,它表示要设置的 Array 元素的第三维索引。

例外
ArgumentException
当前 Array 不是正好具有三个维度。

InvalidCastException
value 不能转换为当前 Array 的元素类型。

ArgumentOutOfRangeException
index1 或 index2 或 index3 超出了当前 Array 的相应维度的有效索引范围。

注解
这些 GetLowerBound 索引和 GetUpperBound 方法可以确定任何索引是否超出边界。

(10)示例

// Array.SetValue 方法
// Array.GetValue 方法
namespace setValue
{
    public class SamplesArray
    {
        public static void Main()
        {
            string[] myArr1 = new string[5];                         // 创建并初始化一维数组 
            myArr1.SetValue("three", 3);                             // 设置索引值=3处设置数组元素值
            Console.WriteLine("[3]:   {0}", myArr1.GetValue(3));     // 输出索引值=3的数组元素

            string[,] myArr2 = new string[5, 5];                     // 创建并初始化二维数组 
            myArr2.SetValue("one-three", 1, 3);                      // 设置索引值 = 1,3处设置数组元素值
            Console.WriteLine("[1,3]:   {0}", myArr2.GetValue(1, 3));// 输出索引值=1,3的数组元素

            string[,,] myArr3 = new string[5, 5, 5];                 // 创建并初始化三维数组 
            myArr3.SetValue("one-two-three", 1, 2, 3);               // 设置索引值=1,2,3处设置数组元素值
            Console.WriteLine("[1,2,3]:   {0}", myArr3.GetValue(1, 2, 3));

            string[,,,,,,] myArr7 = new string[5, 5, 5, 5, 5, 5, 5]; // 创建并初始化7维数组 
            int[] myIndices = [1, 2, 3, 0, 1, 2, 3];                 // 7维数组的一个索引数组
            myArr7.SetValue("one-two-three-zero-one-two-three", myIndices); // 设置索引值=myIndices处设置数组元素值
            Console.WriteLine("[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue(myIndices));
            myArr7.SetValue("one-two-three-zero-one-two-three", 1, 2, 3, 0, 1, 2, 3); // 等效语句
            Console.WriteLine("[1,2,3,0,1,2,3]:   {0}", myArr7.GetValue(1, 2, 3, 0, 1, 2, 3));
        }
    }
}
/*
运行结果:
[3]:   three
[1,3]:   one-three
[1,2,3]:   one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three
[1,2,3,0,1,2,3]:   one-two-three-zero-one-two-three

*/

3.Array.GetValue 方法

????????获取当前 Array 中指定元素的值。

(1)重载

GetValue(Int32)获取一维?Array?中指定位置的值。?索引指定为?32?位整数。
GetValue(Int32[])获取多维?Array?中指定位置的值。?索引指定为一个?32?位整数数组。
GetValue(Int64)获取一维?Array?中指定位置的值。?索引指定为?64?位整数。
GetValue(Int64[])获取多维?Array?中指定位置的值。?索引指定为一个?64?位整数数组。
GetValue(Int32, Int32)获取二维?Array?中指定位置的值。?索引指定为?32?位整数。
GetValue(Int64, Int64)获取二维?Array?中指定位置的值。?索引指定为?64?位整数。
GetValue(Int32, Int32, Int32)获取三维?Array?中指定位置的值。?索引指定为?32?位整数。
GetValue(Int64, Int64, Int64)获取三维?Array?中指定位置的值。?索引指定为?64?位整数。

(2)GetValue(Int32)

?????????获取一维 Array 中指定位置的值。 索引指定为 32 位整数。

public object GetValue (int index);

参数
index
Int32
一个 32 位整数,它表示要获取的 Array 元素的位置。

返回
Object
一维 Array 中指定位置的值。

例外
ArgumentException
当前 Array 不是正好具有一个维度。

IndexOutOfRangeException
index 超出了当前 Array 的有效索引的范围。

注解
和GetLowerBoundGetUpperBound方法可以确定值index是否超出边界。

(3)GetValue(Int32[])

?????????获取多维 Array 中指定位置的值。 索引指定为一个 32 位整数数组。

public object GetValue (params int[] indices);

参数
indices
Int32[]
一个 32 位整数的一维数组,它表示指定要获取的 Array 元素的位置的索引。

返回
Object
多维 Array 中指定位置的值。

例外
ArgumentNullException
indices 为 null。

ArgumentException
当前 Array 中的维数不等于 indices 中的元素数。

IndexOutOfRangeException
indices 中的任何元素都超出了当前 Array 的相应维度的有效索引范围。

注解
方法中的 indices 元素数必须与维度 Array数相等。 数组中的所有 indices 元素必须统一指定多维 Array元素中所需元素的位置。和GetLowerBoundGetUpperBound方法可以确定任何索引是否超出边界。

(4)GetValue(Int64)

?????????获取一维 Array 中指定位置的值。 索引指定为 64 位整数。

public object GetValue (long index);

参数
index
Int64
一个 64 位整数,它表示要获取的 Array 元素的位置。

返回
Object
一维 Array 中指定位置的值。

例外
ArgumentException
当前 Array 不是正好具有一个维度。

ArgumentOutOfRangeException
index 超出了当前 Array 的有效索引的范围。

注解
和GetLowerBoundGetUpperBound方法可以确定值index是否超出边界。

(5)GetValue(Int64[])

?????????获取多维 Array 中指定位置的值。 索引指定为一个 64 位整数数组。

public object GetValue (params long[] indices);

参数
indices
Int64[]
一个 64 位整数的一维数组,它表示指定要获取的 Array 元素的位置的索引。

返回
Object
多维 Array 中指定位置的值。

例外
ArgumentNullException
indices 为 null。

ArgumentException
当前 Array 中的维数不等于 indices 中的元素数。

ArgumentOutOfRangeException
indices 中的任何元素都超出了当前 Array 的相应维度的有效索引范围。

注解
方法中的 indices 元素数必须与维度 Array数相等。 数组中的所有 indices 元素必须统一指定多维 Array元素中所需元素的位置。和GetLowerBoundGetUpperBound方法可以确定任何索引是否超出边界。

(6)GetValue(Int32, Int32)

?????????获取二维 Array 中指定位置的值。 索引指定为 32 位整数。

public object GetValue (int index1, int index2);

参数
index1
Int32
一个 32 位整数,它表示要获取的 Array 元素的第一维索引。

index2
Int32
一个 32 位整数,它表示要获取的 Array 元素的第二维索引。

返回
Object
二维 Array 中指定位置的值。

例外
ArgumentException
当前 Array 不是正好具有两个维度。

IndexOutOfRangeException
index1 或 index2 超出了当前 Array 的相应维度的有效索引范围。

注解
和GetLowerBoundGetUpperBound方法可以确定任何索引是否超出边界。

(7)GetValue(Int64, Int64)

?????????获取二维 Array 中指定位置的值。 索引指定为 64 位整数。

public object GetValue (long index1, long index2);

参数
index1
Int64
一个 64 位整数,它表示要获取的 Array 元素的第一维索引。

index2
Int64
一个 64 位整数,它表示要获取的 Array 元素的第二维索引。

返回
Object
二维 Array 中指定位置的值。

例外
ArgumentException
当前 Array 不是正好具有两个维度。

ArgumentOutOfRangeException
index1 或 index2 超出了当前 Array 的相应维度的有效索引范围。

注解
和GetLowerBoundGetUpperBound方法可以确定任何索引是否超出边界。

(8)GetValue(Int32, Int32, Int32)

????????获取三维 Array 中指定位置的值。 索引指定为 32 位整数。

public object GetValue (int index1, int index2, int index3);

参数
index1
Int32
一个 32 位整数,它表示要获取的 Array 元素的第一维索引。

index2
Int32
一个 32 位整数,它表示要获取的 Array 元素的第二维索引。

index3
Int32
一个 32 位整数,它表示要获取的 Array 元素的第三维索引。

返回
Object
三维 Array 中指定位置的值。

例外
ArgumentException
当前 Array 不是正好具有三个维度。

IndexOutOfRangeException
index1 或 index2 或 index3 超出了当前 Array 的相应维度的有效索引范围。

注解
和GetLowerBoundGetUpperBound方法可以确定任何索引是否超出边界。

(9)GetValue(Int64, Int64, Int64)

????????获取三维 Array 中指定位置的值。 索引指定为 64 位整数。

public object GetValue (long index1, long index2, long index3);

参数
index1
Int64
一个 64 位整数,它表示要获取的 Array 元素的第一维索引。

index2
Int64
一个 64 位整数,它表示要获取的 Array 元素的第二维索引。

index3
Int64
一个 64 位整数,它表示要获取的 Array 元素的第三维索引。

返回
Object
三维 Array 中指定位置的值。

例外
ArgumentException
当前 Array 不是正好具有三个维度。

ArgumentOutOfRangeException
index1 或 index2 或 index3 超出了当前 Array 的相应维度的有效索引范围。

注解
这些 GetLowerBound 索引和 GetUpperBound 方法可以确定任何索引是否超出边界。

4.Array.GetLowerBound(Int32) 方法

????????获取数组中指定维度第一个元素的索引。

(1)定义

public int GetLowerBound (int dimension);

参数
dimension
Int32
数组的从零开始的维度,其起始索引需要确定。

返回
Int32
数组中指定维度第一个元素的索引。

例外
IndexOutOfRangeException
dimension 小于零。
或 -
dimension 等于或大于 Rank。

(2)示例

????????使用 GetLowerBound 和 GetUpperBound 方法显示一维数组和二维数组的边界,并显示其数组元素的值

// 使用 GetLowerBound 和 GetUpperBound 方法显示一维数组和二维数组的边界,
// 并显示其数组元素的值。
namespace GetLowerBound
{
    public class Example
    {
        public static void Main()
        {
            // Create a one-dimensional integer array.
            int[] integers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
            // Get the upper and lower bound of the array.
            int upper = integers.GetUpperBound(0);
            int lower = integers.GetLowerBound(0);
            Console.WriteLine($"Elements from index {lower} to {upper}:");
            // Iterate the array.
            for (int i = lower; i <= upper; i++)
                Console.Write($"{(i == lower ? "   " : "")}{integers[i]}" +
                              $"{(i < upper ? ", " : Environment.NewLine)}");

            Console.WriteLine();

            // Create a two-dimensional integer array.
            int[,] integers2d = { {2, 4}, {3, 9}, {4, 16}, {5, 25},
                           {6, 36}, {7, 49}, {8, 64}, {9, 81} };
            // Get the number of dimensions.
            int rank = integers2d.Rank;
            Console.WriteLine($"Number of dimensions: {rank}");
            for (int i = 0; i < rank; i++)
                Console.WriteLine($"   Dimension {i}: " +
                                  $"from {integers2d.GetLowerBound(i)} to {integers2d.GetUpperBound(i)}");

            // Iterate the 2-dimensional array and display its values.
            Console.WriteLine("   Values of array elements:");
            for (int outer = integers2d.GetLowerBound(0); outer <= integers2d.GetUpperBound(0);
                 outer++)
                for (int inner = integers2d.GetLowerBound(1); inner <= integers2d.GetUpperBound(1);
                     inner++)
                    Console.WriteLine($"      {'\u007b'}{outer}, {inner}{'\u007d'} = " +
                                      $"{integers2d.GetValue(outer, inner)}");
        }
    }
}
// 运行结果:
/*
Elements from index 0 to 9:
   2, 4, 6, 8, 10, 12, 14, 16, 18, 20

Number of dimensions: 2
   Dimension 0: from 0 to 7
   Dimension 1: from 0 to 1
   Values of array elements:
      {0, 0} = 2
      {0, 1} = 4
      {1, 0} = 3
      {1, 1} = 9
      {2, 0} = 4
      {2, 1} = 16
      {3, 0} = 5
      {3, 1} = 25
      {4, 0} = 6
      {4, 1} = 36
      {5, 0} = 7
      {5, 1} = 49
      {6, 0} = 8
      {6, 1} = 64
      {7, 0} = 9
      {7, 1} = 81

 */

5.Array.GetUpperBound(Int32) 方法

????????获取数组中指定维度最后一个元素的索引。

public int GetUpperBound (int dimension);

参数
dimension
Int32
数组的从零开始的维度,其上限需要确定。

返回
Int32
数组中指定维度最后一个元素的索引,或 -1(如果指定维度为空)。

例外
IndexOutOfRangeException
dimension 小于零。
或 -
dimension 等于或大于 Rank。

文章来源:https://blog.csdn.net/wenchm/article/details/135467113
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。