Tags: arrays, concatenate, concatenating, easiest, example, match, net, offiles, patterns, search, sharp

Concatenating arrays

On .Net » .Net C# (C sharp)

6,593 words with 5 Comments; publish: Mon, 31 Dec 2007 13:32:00 GMT; (10062.50, « »)

What's the easiest way to concatenate arrays ? For example, I want a list of

files that match one of 3 search patterns, so I need something like

DirectoryInfo ld = new DirectoryInfo(searchDir);

pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") +

ld.GetFiles("*.master.resx");

but of course there is no + operation allowed on the FileInfo[] arrays

returned by the GetFiles method.

Do I have to read into 3 separate arrays then copy each entry one at a time

into a 4th array ?? yuk. Must be a neater way.

All Comments

Leave a comment...

  • 5 Comments
    • Hi,

      Well the first thought to cross my mind is create an array of array, then

      you can iterate them using two foreach, at the end it will have the same

      performance that if you have only one concatenated.

      If this is not suitable for you, you can create a fourth array big enough

      for contain the other three using the Array.CopyTo method

      Cheers,

      --

      Ignacio Machin,

      ignacio.machin AT dot.state.fl.us

      Florida Department Of Transportation

      "JezB" <jezbroadsword.net-csharp.todaysummary.com.blueyonder.co.uk> wrote in message

      news:%232FwDrpiEHA.3016.net-csharp.todaysummary.com.tk2msftngp13.phx.gbl...

      > What's the easiest way to concatenate arrays ? For example, I want a list

      of

      > files that match one of 3 search patterns, so I need something like

      > DirectoryInfo ld = new DirectoryInfo(searchDir);

      > pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") +

      > ld.GetFiles("*.master.resx");

      > but of course there is no + operation allowed on the FileInfo[] arrays

      > returned by the GetFiles method.

      > Do I have to read into 3 separate arrays then copy each entry one at a

      time

      > into a 4th array ?? yuk. Must be a neater way.

      #1; Mon, 31 Dec 2007 13:33:00 GMT
    • JezB wrote:

      > What's the easiest way to concatenate arrays ? For example, I want a list of

      > files that match one of 3 search patterns, so I need something like

      > DirectoryInfo ld = new DirectoryInfo(searchDir);

      > pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") +

      > ld.GetFiles("*.master.resx");

      > but of course there is no + operation allowed on the FileInfo[] arrays

      > returned by the GetFiles method.

      > Do I have to read into 3 separate arrays then copy each entry one at a time

      > into a 4th array ?? yuk. Must be a neater way.

      You could use an ArrayList and fill that and finally convert it to an Array:

      DirectoryInfo dirInfo = new DirectoryInfo(.net-csharp.todaysummary.com."C:\whereever\whatever");

      ArrayList fileInfoList = new ArrayList(dirInfo.GetFiles(.net-csharp.todaysummary.com."*.html"));

      fileInfoList.AddRange(dirInfo.GetFiles(.net-csharp.todaysummary.com."*.aspx"));

      object[] fileInfos = fileInfoList.ToArray();

      Console.WriteLine(fileInfos.Length);

      but you get an object[] Array then in .NET 1.0/1.1.

      --

      Martin Honnen

      http://JavaScript.FAQTs.com/

      #2; Mon, 31 Dec 2007 13:34:00 GMT
    • JezB <jezbroadsword.net-csharp.todaysummary.com.blueyonder.co.uk> wrote:

      > What's the easiest way to concatenate arrays ? For example, I want a list of

      > files that match one of 3 search patterns, so I need something like

      > DirectoryInfo ld = new DirectoryInfo(searchDir);

      > pfiles = ld.GetFiles("*.aspx.resx|") + ld.GetFiles("*.ascx.resx") +

      > ld.GetFiles("*.master.resx");

      > but of course there is no + operation allowed on the FileInfo[] arrays

      > returned by the GetFiles method.

      > Do I have to read into 3 separate arrays then copy each entry one at a time

      > into a 4th array ?? yuk. Must be a neater way.

      Well, you don't need to do things "one a a time" - you can use

      Array.Copy to avoid that. Here's a sample which might help you. Note

      that it doesn't try to deal with (or even detect) multi-dimensional

      arrays, or those with a non-zero lower bound.

      using System;

      class Test

      {

      static void Main()

      {

      string[] first = {"hello", "there"};

      string[] second = {"a", "b", "c"};

      int[] third = {1, 2, 3};

      // This will fail

      // string[] ret = (string[]) ConcatenateArrays(first, second, third);

      string[] ret = (string[]) ConcatenateArrays(first, second);

      foreach (string x in ret)

      {

      Console.WriteLine (x);

      }

      }

      static Array ConcatenateArrays(params Array[] arrays)

      {

      if (arrays==null)

      {

      throw new ArgumentNullException("arrays");

      }

      if (arrays.Length==0)

      {

      throw new ArgumentException("No arrays specified");

      }

      Type type = arrays[0].GetType().GetElementType();

      int totalLength = arrays[0].Length;

      for (int i=1; i < arrays.Length; i++)

      {

      if (arrays[i].GetType().GetElementType() != type)

      {

      throw new ArgumentException

      ("Arrays must all be of the same type");

      }

      totalLength += arrays[i].Length;

      }

      Array ret = Array.CreateInstance(type, totalLength);

      int index=0;

      foreach (Array array in arrays)

      {

      Array.Copy (array, 0, ret, index, array.Length);

      index += array.Length;

      }

      return ret;

      }

      }

      --

      Jon Skeet - <skeet.net-csharp.todaysummary.com.pobox.com>

      http://www.pobox.com/~skeet

      If replying to the group, please do not mail me too

      #3; Mon, 31 Dec 2007 13:35:00 GMT
    • Martin Honnen <mahotrash.net-csharp.todaysummary.com.yahoo.de> wrote:

      > You could use an ArrayList and fill that and finally convert it to an Array:

      > DirectoryInfo dirInfo = new DirectoryInfo(.net-csharp.todaysummary.com."C:\whereever\whatever");

      > ArrayList fileInfoList = new ArrayList(dirInfo.GetFiles(.net-csharp.todaysummary.com."*.html"));

      > fileInfoList.AddRange(dirInfo.GetFiles(.net-csharp.todaysummary.com."*.aspx"));

      > object[] fileInfos = fileInfoList.ToArray();

      > Console.WriteLine(fileInfos.Length);

      > but you get an object[] Array then in .NET 1.0/1.1.

      Unless you use ArrayList.ToArray(Type) of course, and cast the

      result...

      --

      Jon Skeet - <skeet.net-csharp.todaysummary.com.pobox.com>

      http://www.pobox.com/~skeet

      If replying to the group, please do not mail me too

      #4; Mon, 31 Dec 2007 13:36:00 GMT
    • Jon Skeet [C# MVP] wrote:

      > Martin Honnen <mahotrash.net-csharp.todaysummary.com.yahoo.de> wrote:

      >> object[] fileInfos = fileInfoList.ToArray();

      >>

      >>but you get an object[] Array then in .NET 1.0/1.1.

      >

      > Unless you use ArrayList.ToArray(Type) of course, and cast the

      > result...

      I somehow managed to miss that overload of ToArray, then it should be

      fine to use the ArrayList.

      --

      Martin Honnen

      http://JavaScript.FAQTs.com/

      #5; Mon, 31 Dec 2007 13:37:00 GMT