将2个arraylists添加到一个二维数组中

时间:2014-06-19 09:18:26

标签: c# arrays arraylist

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

我的代码:

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();

我在向数组中添加arraylists时遇到了问题,还是在dotnet highcharts中?

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

您也可以尝试List<KeyValuePair<Date,int>>并从那里填充您的2D数组,或者如果日期是唯一的,Dictionary<Date, int>