【问题标题】:Adding 2 arraylists to one 2 dimensional array将 2 个数组列表添加到一个二维数组
【发布时间】:2014-06-19 09:18:25
【问题描述】:

我正在尝试将两个数组列表添加到二维数组中,但遇到了问题。如果我手动填充数组,在下面的代码中显示为数组 arr1,并将此数组绑定到图表,图表将按预期显示。但是,当我使用 for 循环将两个数组列表添加到数组并将此数组绑定到图表时,它不会按预期显示。

我的代码:

DateTime date1 = new DateTime(2012, 10, 1);
DateTime date2 = new DateTime(2012, 11, 2);
DateTime date3 = new DateTime(2012, 12, 3);
DateTime date4 = new DateTime(2013, 01, 4);
DateTime date5 = new DateTime(2013, 02, 8);

//Create time arraylist
ArrayList al1 = new ArrayList();
al1.Add(date1);
al1.Add(date2);
al1.Add(date3);
al1.Add(date4);
al1.Add(date5);

int int1 = 9;
int int2 = 15;
int int3 = 20;
int int4 = 13;
int int5 = 17;

//Create int arraylist
ArrayList al2 = new ArrayList();
al2.Add(int1);
al2.Add(int2);
al2.Add(int3);
al2.Add(int4);
al2.Add(int5);

//Tester 2D array
Object[,] arr1 = new Object[,] { {date1, int1}, {date2, int2}, {date3, int3}, {date4,int4}, {date5, int5} };

//Create 2D array
Object[,] arr = new Object[2, al2.Count];

for (int k = 0; k <al2.Count; k++)
        {
            arr[0, k] = al1[k];
            arr[1, k] = al2[k];
        }

Data d1 = new Data(arr);

Series s1 = new Series { Name = "Series 1", Data=d1};

DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart");

chart.SetXAxis(new XAxis
{
Type = AxisTypes.Datetime
});

chart.SetSeries(s1);

ltrChart.Text = chart.ToHtmlString();

将数组列表添加到数组中是我的问题还是 dotnet highcharts 中的问题?

【问题讨论】:

  • 这两个结果是什么。预期结果是什么,实际结果是什么?
  • 预期结果是时间与 int 的折线图,{ {{1/10/2012, 9} {2/11/2012, 15} etc },这是我使用时得到的arr1 数组。我使用 arr 数组时的结果只是图上的两个点,{ {1/1/1970, 15}, {30/09/2012, 131581440000} }
  • 看看this tutorial
  • 除非你卡在 .Net 2.0 之前,否则不要使用 ArrayList,如果“HighCharts”只接受多维 ArrayList,不要使用“HighCharts”。

标签: c# arrays arraylist


【解决方案1】:

您的循环创建了一个不同的数组,因为您混淆了维度。这样做:

Object[,] arr = new Object[al2.Count, 2];

for (int k = 0; k < al2.Count; k++)
{
    arr[k, 0] = al1[k];
    arr[k, 1] = al2[k];
}

【讨论】:

  • 您也可以检查 al1.Count == al2.Count,否则可能会引发异常。
  • @fabigler 他在代码中手动填充它们,它们怎么会有不同的大小?即使他没有,如果他们这样做了,其他地方也会有错误。然后修复它。
【解决方案2】:

您也可以尝试使用List&lt;KeyValuePair&lt;Date,int&gt;&gt; 并从那里填充您的二维数组,或者如果日期是唯一的,则使用Dictionary&lt;Date, int&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2017-12-22
    • 2012-01-01
    • 2018-07-22
    相关资源
    最近更新 更多