Concatenating arrays

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

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.
 
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,
 
JezB said:
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(@"C:\whereever\whatever");
ArrayList fileInfoList = new ArrayList(dirInfo.GetFiles(@"*.html"));
fileInfoList.AddRange(dirInfo.GetFiles(@"*.aspx"));
object[] fileInfos = fileInfoList.ToArray();
Console.WriteLine(fileInfos.Length);

but you get an object[] Array then in .NET 1.0/1.1.
 
JezB said:
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.GetType().GetElementType() != type)
{
throw new ArgumentException
("Arrays must all be of the same type");
}
totalLength += arrays.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;
}
}
 
Martin Honnen said:
You could use an ArrayList and fill that and finally convert it to an Array:

DirectoryInfo dirInfo = new DirectoryInfo(@"C:\whereever\whatever");
ArrayList fileInfoList = new ArrayList(dirInfo.GetFiles(@"*.html"));
fileInfoList.AddRange(dirInfo.GetFiles(@"*.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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top