将数据保存在字符串数组中

时间:2014-04-17 07:56:09

标签: c# arrays

我是c#的新手。我必须定义多个数组来保存datagridview数据。 如何定义在一个数组中定义的多个字符串数组?

6 个答案:

答案 0 :(得分:2)

为什么要将数据保存在字符串数组中?改为使用自定义对象的datatableList

答案 1 :(得分:1)

取自here。只需将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 } } };

答案 2 :(得分:1)

你可以这样做使用Jagged array

string[] a1 = { "1", "2", "3"};
string[] a2 = { "4", "5", "6"};


string[][] arr = {a1, a2};

答案 3 :(得分:1)

您可以使用列表,如下所示。但是为什么要使用字符串数组呢?而不是这个你可以使用具有已定义属性的类。

List<string[]> asdf = new List<string[]>();

答案 4 :(得分:1)

您需要使用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
*/

答案 5 :(得分:1)

你可以简单地使用ArrayList来获得你想要的东西