1

I have two arrays, which are to be used as x-axis inputs on a chart using ChartDirector(http://www.advsofteng.com/product.html) in my C#/.NET Windows Forms application.

One of the arrays does not match up in length with the other, resulting in a haphazard display of the shorter array on the chart. I need to create a new array that takes the short one as an input and fill the remaining values as 0 equivalents (Chart.NoValue) here, so that the lengths of both arrays are equal.

Is there a way to do this without copying the short array into a new one and filling the remaining values with 0's, as follows:

double[] newArray = new double[longArray.Length];
for (int i = 0; i < shortArray.Length; i++)
{
    newArray[i] = shortArray[i];
}
for (i = shortArray.Length; i < newArray.Length; i++)
{
    newArray[i] = Chart.NoValue; 
}
//Chart director equivalent of 0, in terms of chart visibility

Essentially, I'm asking if there is a way to just modify shortArray in such a way that new "0" elements are added at the end so that it's length equates to longArray.

John
  • 179
  • 2
  • 10

3 Answers3

4

You could use the Array.Resize< T> Method http://msdn.microsoft.com/en-us/library/bb348051.aspx to resize the array.

If newSize is greater than the Length of the old array, a new array is allocated and all the elements are copied from the old array to the new one. If newSize is less than the Length of the old array, a new array is allocated and elements are copied from the old array to the new one until the new one is filled; the rest of the elements in the old array are ignored. If newSize is equal to the Length of the old array, this method does nothing. source: http://msdn.microsoft.com/en-us/library/bb348051.aspx

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • This works exactly as I need it to. But, out of curiousity, is it possible to use some sort of [Linq-type expression](http://stackoverflow.com/questions/11789750/replace-all-occurences-of-a-string-from-a-string-array) to replace all the 0's added at the end of my re-sized double array with "Chart.NoValue"? Otherwise, should I just use my 2nd for-loop above? – John Sep 02 '13 at 10:56
  • You could do something like using the SetValue. this: _newArray.SetValue(Chart.NoValue, Enumerable.Range(index, count).ToArray());_ And using the Enumerable.Range to create a list of indices. See: _Array.SetValue Method (Object, Int32[])_ http://msdn.microsoft.com/en-us/library/758awxk7.aspx) – Jeroen van Langen Sep 02 '13 at 11:41
1

Arrays in C# have fixed length, so you cannot mutate the existing instance. You do have to create a new array "and fill it up" with Chart.NoValue.

However, you can replace the first loop with the equivalent (and faster)

shortArray.CopyTo(newArray, 0);
Jon
  • 428,835
  • 81
  • 738
  • 806
1
double[] newArray = new double[longArray.Length];
Array.Copy(shortArray, newArray, shortArray.Length);

If the new elements must be Chart.NoValue instead of 0 then here is another solution:

double[] newArray = Enumerable.Repeat(Chart.NoValue, longArray.Length).ToArray();
Array.Copy(shortArray, newArray, shortArray.Length);
aligray
  • 2,812
  • 4
  • 26
  • 35