将数据保留在字符串数组中
本文关键字:数组 字符串 数据 保留 | 更新日期: 2023-09-27 17:58:47
我是c#的新手。我必须定义多个数组来保留datagridview
数据。如何定义在一个数组中定义的多个字符串数组?
为什么要将数据保留在字符串数组中?请改用自定义对象的datatable或List。
您可以简单地使用ArrayList
来获得您想要的
取自此处。只需将int
替换为string
s。
// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
{ "five", "six" } };
// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
通过使用锯齿阵列,您可以这样做
string[] a1 = { "1", "2", "3"};
string[] a2 = { "4", "5", "6"};
string[][] arr = {a1, a2};
您可以使用如下列表。但是为什么要使用字符串数组呢?相反,您可以使用具有已定义属性的类。
List<string[]> asdf = new List<string[]>();
您需要使用Jagged数组。
看看这个,它包含了它们是什么以及如何使用它们的解释和示例:
http://msdn.microsoft.com/en-us/library/2s05feca.aspx
只要用字符串替换int,就可以开始了。
链接示例:
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
/* Output:
Element(0): 1 3 5 7 9
Element(1): 2 4 6 8
*/