如何返回锯齿状数组 [英] How to return a jagged array

查看:62
本文介绍了如何返回锯齿状数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,它使用 2D 锯齿状数组来保存 SQL 查询中的记录.

I have a function which use a 2D jagged array to save records from an SQL query.

如何正确返回锯齿状数组?

How do return the jagged array correctly?

我尝试了类似的东西:

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

在我的主程序中:

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.

在您的代码中:

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

您首先将一个新数组分配给 test,然后用 GetResult() 的返回值覆盖它.代码与以下内容相同:

You first assign a new array to test, then overwrite it with the return value from GetResult(). The code does the same as:

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

现在 GetResult() 应该返回一个 string[][] - 试试这个来感受一下使用锯齿状数组的感觉:

Now the GetResult() should return a string[][] - try this to get a feel of working with jagged arrays:

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;
}

您可以向该方法提供对 SQL 操作结果的引用,以便它可以访问数据,将其转换"为 string[][].

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[][].

这篇关于如何返回锯齿状数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆