简体   繁体   English

如何返回锯齿状数组

[英]How to return a jagged array

I have a function which use a 2D jagged array to save records from an SQL query. 我有一个函数,该函数使用2D锯齿状数组来保存SQL查询中的记录。

How do return the jagged array correctly? 如何正确返回锯齿状数组?

I tried something like: 我尝试了类似的东西:

public string[][] GetResult()
{
    return result;
}

And in my main programm: 在我的主程序中:

string[][] test = new string[server1.GetResult().Length][];
test = server1.GetResult();

Well, as expected, it didn't work. 好了,正如预期的那样,它没有用。

I don't know how to fix my problem. 我不知道如何解决我的问题。

Jagged arrays are simply arrays of arrays. 锯齿状的数组只是数组的数组。

In your code: 在您的代码中:

string[][] test = new string[server1.GetResult().Length][];
test = Gronforum.GetResult();

You first assign a new array to test , then overwrite it with the return value from GetResult() . 您首先分配一个新数组进行test ,然后用GetResult()的返回值覆盖它。 The code does the same as: 该代码与以下代码相同:

string[][] test = Gronforum.GetResult();

Now the GetResult() should return a string[][] - try this to get a feel of working with jagged arrays: 现在, GetResult()应该返回一个string[][] -尝试使用这种方法来处理锯齿状数组:

public string[][] GetResult()
{
    string[][] result = new string[2][];
    result[0] = new string[] { "1", "2" };
    result[1] = new string[2];
    result[1][0] = "a";
    result[1][1] = "b";
    return result;
}

You could supply a reference to the result of the SQL operation to that method so it has access to the data, to "convert" it to a string[][] . 您可以提供对该方法的SQL操作结果的引用,以便它可以访问数据,以将其“转换”为string[][]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM