-1

I have a string array with 125 columns:

string[] arrColumns = null;
...populate array here
field0 = arrColumns[0];
field1 = arrColumns[1];
...etc to 124
...modify array here to add a new column to the beginning of the string array

I need to add a new column to the beginning of the array.

So that arrColumns[0] is moved to arrColumns[1]; etc And then I will have a new column arrColumns[0] to add data into. Can this be done easily in C#? Thanks before hand.

pes502
  • 1,597
  • 3
  • 17
  • 32
user3657279
  • 188
  • 2
  • 12
  • 6
    Sounds like you want a `List` instead of a `string[]`... then you can insert wherever you like. – Jon Skeet Jul 14 '14 at 15:05
  • Here's an existing answer: http://stackoverflow.com/questions/11402238/prepend-to-a-c-sharp-array – jmsb Jul 14 '14 at 15:05
  • The easiest way would be to iterate over all the fields in reverse (so from 124 to 0), move them by 1 (so from 125 to 1), then add your new column to arrColumn[0]. Or you can use array manipulation to create a new array, define arrColumns[0], then append your old array. – Nzall Jul 14 '14 at 15:07

3 Answers3

2

The approach closest to what you are describing is to use the Array.Copy method:

string[] arrColumns = new string[1024];
string[] newArray = new string[arrColumns.Length + 1];

Array.Copy(arrColumns, 0, newArray, 1, arrColumns.Length);
newArray[0] = "Hello, world!

This is a little long winded, especially if you keep doing this. A better option is to not worry about the details of copying and inserting a value in the first position, by using a List<T> and go from there.

There are 3 options to improve this:

  1. Use a List<T> with a capacity
  2. Use a List<T> with the original array
  3. Create a List<T> from the existing array

Use a List<T> with a capacity

Use the List<string> that accepts a capacity in the constructor, so that you give it a heads up (where possible) on the size to create.

It will then allocate the initial size internally, but still know the number of items stored in the .Count, rather than the .Capacity. It is also dynamically expanding:

List<string> listOfColumns = new List<string>(1024); // Set initial capacity
listOfColumns.Add( .... );

Use a List<T> with the original array

Another alternative is to just pass the array directly into the constructor, and you can use the overload to pass an existing array in:

List<string> listOfColumns = new List<string>(arrColumns);

Create a List<T> from the existing array

Use the System.Linq extension to create a List<T> automatically:

List<string> listOfColumns = arrColumns.ToList();

Then you can insert an item into the beginning of the list.

listOfColumns.Insert(0, "Hello, World!");

To get the data as an array, use the .ToArray() method.

string[] arrColumns = listOfColumns.ToArray();
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
0

You could copy to a List<string>...

string[] strArray = new string[99];
strArray[0] = "1";
strArray[1] = "2";
strArray[2] = "3";

var strList = new List<string>();
strList.Add("0");
strList.AddRange(strArray.Where(a => !string.IsNullOrWhiteSpace(a)));

This will create a new list with whatever you add at the first position, and then add the records from the array where the element isn't empty.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
0

If it should be an array you can use this

// Create a temporary array
string[] tmp = (string[])arrColumns.Clone();

// Take the first items and copy to temp
arrColumns.Take(arrColumns.Length-1).ToArray().CopyTo(tmp, 1);

// Add new element at position 0
tmp[0] = "New";

// Copy back
tmp.CopyTo(arrColumns);

This will shift your data one position to the end and delete the last entry. The size of the array will not change.

Mathias Müller
  • 303
  • 3
  • 12