📌  相关文章
📜  C#中的Array.GetValue()方法(含示例)套装– 3

📅  最后修改于: 2021-05-29 19:17:57             🧑  作者: Mango

C#中的Array.GetValue()方法用于获取当前Array中指定元素的值。此方法的重载列表中共有8个方法,如下所示:

  • Array.GetValue(Int32,Int32)
  • Array.GetValue(Int64,Int64)
  • Array.GetValue(Int32)
  • Array.GetValue(Int64)
  • Array.GetValue(Int32,Int32,Int32)
  • Array.GetValue(Int64,Int64,Int64)
  • Array.GetValue(Int32 [])
  • Array.GetValue(Int64 [])

在本文中,我们将解释Array.GetValue(Int32,Int32,Int32)Array.GetValue(Int64,Int64,Int64)方法。

Array.GetValue(Int32,Int32,Int32)方法用于获取三维数组中指定位置的值。索引被指定为32位整数。

句法:

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

这里,

  • “ index1”是元素所在的数组的编号。
  • “ index2”是元素所在行的索引。
  • “ index3”是元素所在列的索引。

返回值:该方法返回指定索引处的元素,该索引由3D数组中传递的参数定义。该值是一个32位整数值。

例外情况:

  • ArgumentException:当前的Array不完全具有三个维度。
  • IndexOutOfRangeException:如果index1或index2或index3在当前数组的相应维的有效索引范围之外。

Array.GetValue(Int64,Int64,Int64)方法用于获取三维数组中指定位置的值。索引被指定为64位整数。

句法:

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

这里,

  • “ index1”是元素所在的数组的编号。
  • “ index2”是元素所在行的索引。
  • “ index3”是元素所在列的索引。

返回值:该方法返回指定索引处的元素,该索引由3D数组中传递的参数定义。该值是64位整数值。

例外情况:

  • ArgumentException:当前的Array不完全具有三个维度。
  • IndexOutOfRangeException:如果index1或index2或index2在当前数组的相应维的有效索引范围之外。

范例1:

C#
// C# program to demonstrate
// Array.GetValue(Int32, Int32, Int32)
// and array.GetValue(Int64, Int64, Int64)
// method
using System;
public class GFG {
  
public static void Main() {
  
    // declare a character
    // array of size 2x2x3
    // 2 row and 3 column
    // numbe of array is 2
    char[, , ] arr = new char[2, 2, 3]{
  
        {// array 1
         {'A', 'B', 'C'},
         {'D', 'E', 'F'}
  
        },
        {// array 2
         {'G', 'H', 'I'},
         {'J', 'K', 'L'}}
  
    };
  
    // using GetValue(Int32, Int32, Int32) and
    // GetValue(Int64, Int64, Int64) method
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < 3; k++) {
          Console.WriteLine("element at index [{0}, {1}, {2}] is : {3}", i, j,
                            k, arr.GetValue(i, j, k));
        }
      }
    }
  }
}


C#
// C# program to demonstrate
// Array.GetValue(Int32, Int32, Int32)
// and array.GetValue(Int64, Int64, Int64)
// method
using System;
  
public class GFG {
  
    public static void Main()
    {
        // declare a character
          // array of size 2x2x3
        // 2 row and 3 column
        // numbe of array is 2
        string[,, ] arr = new string[2, 2, 3];
  
        // use "SetValue()" method to set
        // the value at specified index
        arr.SetValue("C++", 0, 0, 0);
        arr.SetValue("Java", 0, 0, 1);
        arr.SetValue("C#", 0, 0, 2);
        arr.SetValue("Perl", 0, 1, 0);
        arr.SetValue("Python", 0, 1, 1);
        arr.SetValue("PHP", 0, 1, 2);
        arr.SetValue("GO", 1, 0, 0);
        arr.SetValue("Ruby", 1, 0, 1);
        arr.SetValue("F#", 1, 0, 2);
        arr.SetValue("JavaScript", 1, 1, 0);
        arr.SetValue("SQL", 1, 1, 1);
        arr.SetValue("kotlin", 1, 1, 2);
  
        // using GetValue(Int32, Int32, Int32) and
        // GetValue(Int64, Int64, Int64) method
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 3; k++) {
                    Console.WriteLine("element at index [{0}, {1}, {2}] is : {3}",
                                                  i, j, k, arr.GetValue(i, j, k));
                }
            }
        }
    }
}


输出
element at index [0, 0, 0] is : A
element at index [0, 0, 1] is : B
element at index [0, 0, 2] is : C
element at index [0, 1, 0] is : D
element at index [0, 1, 1] is : E
element at index [0, 1, 2] is : F
element at index [1, 0, 0] is : G
element at index [1, 0, 1] is : H
element at index [1, 0, 2] is : I
element at index [1, 1, 0] is : J
element at index [1, 1, 1] is : K
element at index [1, 1, 2] is : L

范例2:

C#

// C# program to demonstrate
// Array.GetValue(Int32, Int32, Int32)
// and array.GetValue(Int64, Int64, Int64)
// method
using System;
  
public class GFG {
  
    public static void Main()
    {
        // declare a character
          // array of size 2x2x3
        // 2 row and 3 column
        // numbe of array is 2
        string[,, ] arr = new string[2, 2, 3];
  
        // use "SetValue()" method to set
        // the value at specified index
        arr.SetValue("C++", 0, 0, 0);
        arr.SetValue("Java", 0, 0, 1);
        arr.SetValue("C#", 0, 0, 2);
        arr.SetValue("Perl", 0, 1, 0);
        arr.SetValue("Python", 0, 1, 1);
        arr.SetValue("PHP", 0, 1, 2);
        arr.SetValue("GO", 1, 0, 0);
        arr.SetValue("Ruby", 1, 0, 1);
        arr.SetValue("F#", 1, 0, 2);
        arr.SetValue("JavaScript", 1, 1, 0);
        arr.SetValue("SQL", 1, 1, 1);
        arr.SetValue("kotlin", 1, 1, 2);
  
        // using GetValue(Int32, Int32, Int32) and
        // GetValue(Int64, Int64, Int64) method
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 3; k++) {
                    Console.WriteLine("element at index [{0}, {1}, {2}] is : {3}",
                                                  i, j, k, arr.GetValue(i, j, k));
                }
            }
        }
    }
}
输出
element at index [0, 0, 0] is : C++
element at index [0, 0, 1] is : Java
element at index [0, 0, 2] is : C#
element at index [0, 1, 0] is : Perl
element at index [0, 1, 1] is : Python
element at index [0, 1, 2] is : PHP
element at index [1, 0, 0] is : GO
element at index [1, 0, 1] is : Ruby
element at index [1, 0, 2] is : F#
element at index [1, 1, 0] is : JavaScript
element at index [1, 1, 1] is : SQL
element at index [1, 1, 2] is : kotlin

注意:对于在线编译器,不能使用32位或64位整数。将脱机编译器用于32或64位整数。