472,143 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Concatenating arrays

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
5 29110
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


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
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
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


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.

Similar topics

5 posts views Thread by Unforgiven | last post: by
14 posts views Thread by foodic | last post: by
7 posts views Thread by War Eagle | last post: by
4 posts views Thread by Richard L Rosenheim | last post: by
1 post views Thread by Sheldon | last post: by
21 posts views Thread by c | last post: by
1 post views Thread by Rolf Wester | last post: by
8 posts views Thread by arnuld | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.