473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 29219
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Unforgiven | last post by:
I have an application, where I continuously get new binary data input, in the form of a char*. This data comes from the Windows Multimedia wave input functions, but that's not important. What it...
14
by: foodic | last post by:
i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;} void setY(int y) {_y = y;} void...
4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I canīt see...
7
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. ...
4
by: Richard L Rosenheim | last post by:
Is there any built-in method or mechanism for concatenating two arrays of byte together? I haven't come across anything to do this, and was just checking before I implement some code. Richard...
1
by: Sheldon | last post by:
Good day everyone, I would like to know if anyone has a fast and concise way of concatenating two 2D arrays of same dimensions? Whenever I try: a = concatenate(b,c) I get erroneous data as...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
1
by: Rolf Wester | last post by:
Hi, I want to concatenate two numpy arrays with shape (n1,n2) and (n1,n3) into a single array with shape (n1,n2+n3). I guess there is an elegant way to do this but I couldn't figure it out. So...
8
by: arnuld | last post by:
i tried to output these 2 to the std. output: const std::string hello = "Hello"; const std::string message = hello + ", world" + "!"; const std::string exclam = "!"; const std::string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.