English 中文(简体)
如何通过两个层面阵列加以振兴
原标题:How to iterate through two dimensional array

我有两个名单:我把他们改成像以下两个分水阵:

double[,] data = new double[voltage.Count(), 2];
for (int i = 0; i < voltage.Count(); i++)
{
    data[i, 0] = voltage[i];
    data[i, 1] = current[i];
}

现在,我正试图通过这一阵列敲响,但我对每一行的电压和流体都具有同样的价值:

foreach (double data in ztr.GetCurveDataForTestType()) //this will return my array
{
    richTextBox1.AppendText("Voltage" + data + "    ---------    ");
    richTextBox1.AppendText("Current" + data + "
");
}

Voltage-0.175 --------- Current-0.175
Voltage-9.930625E-06 --------- Current-9.930625E-06
Voltage-0.171875 --------- Current-0.171875
Voltage-9.53375E-06 --------- Current-9.53375E-06
Voltage-0.16875 --------- Current-0.16875

As you see in the first line both voltage and current are same value that is voltage, and on the second raw they are the same again but this time it is the current value. How can I fix this?

最佳回答

我就此建议使用多维阵列。

相反,你可以做这样的一类:

class Measurement
{
    public double Voltage { get; set; }
    public double Current { get; set; }
}

并改变您的返回方法<代码> IE amountable<Measurement>。 在NET4.0中,可使用<代码>Zip:

public IEnumerable<Measurement> GetCurveDataForTestType()
{
    return voltage.Zip(current,
        (v, c) => new Measurement { Voltage = v, Current = c});
}

旧版本。 NET:

public IEnumerable<Measurement> GetCurveDataForTestType()
{
    for (int i = 0; i < voltage.Count(); i++)
    {
        yield return new Measurement
        {
            Voltage = voltage[i],
            Current = current[i]
        };
    }
}

之后,你的法典成为:

foreach (Measurement data in ztr.GetCurveDataForTestType())
{
    richTextBox1.AppendText(
        "Voltage: {0} ---------  Current: {1}", data.Voltage, data.Current);
}
问题回答

你使用与你制造阵列时相同的 lo:

for (int i = 0; i < data.GetLength(0); i++) {
  richTextBox1.AppendText("Voltage" + data[i, 0] + "    ---------    ");
  richTextBox1.AppendText("Current" + data[i, 1] + "
");
}

Update: This question appears to be a duplicate of How to foreach through a 2 dimensional array?. The linked answer has an alternate solution that enables the use of foreach by switching from a two-dimensional array to an array of array.


foreach with a two aspectsal range, will sequentially re each Double Value in the range (by Design).

例如,鉴于以下两维的双重:

var array = new double[,] { { 0, 1, }, { 2, 3 }, { 4, 5 } };

foreach (var d in array)
{
    Console.WriteLine(d);
}

您将获得以下青少年产出:

0
1
2
3
4
5

www.un.org/Depts/DGACM/index_spanish.htm 因此,对你的需求并不合适。 页: 1





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签