By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,334 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,334 IT Pros & Developers. It's quick & easy.

Concatenating arrays

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <je***********@blueyonder.co.uk> wrote in message
news:%2****************@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.

Nov 16 '05 #2

P: n/a


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(@"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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 16 '05 #3

P: n/a
JezB <je***********@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
Martin Honnen <ma*******@yahoo.de> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a


Jon Skeet [C# MVP] wrote:
Martin Honnen <ma*******@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/
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.