473,738 Members | 6,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s earchDir);
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 29248
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.u k> wrote in message
news:%2******** ********@tk2msf tngp13.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(s earchDir);
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(s earchDir);
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\w hatever");
ArrayList fileInfoList = new ArrayList(dirIn fo.GetFiles(@"* .html"));
fileInfoList.Ad dRange(dirInfo. GetFiles(@"*.as px"));
object[] fileInfos = fileInfoList.To Array();
Console.WriteLi ne(fileInfos.Le ngth);

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.u k> 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(s earchDir);
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[]) ConcatenateArra ys(first, second, third);

string[] ret = (string[]) ConcatenateArra ys(first, second);
foreach (string x in ret)
{
Console.WriteLi ne (x);
}
}

static Array ConcatenateArra ys(params Array[] arrays)
{
if (arrays==null)
{
throw new ArgumentNullExc eption("arrays" );
}
if (arrays.Length= =0)
{
throw new ArgumentExcepti on("No arrays specified");
}

Type type = arrays[0].GetType().GetE lementType();
int totalLength = arrays[0].Length;
for (int i=1; i < arrays.Length; i++)
{
if (arrays[i].GetType().GetE lementType() != type)
{
throw new ArgumentExcepti on
("Arrays must all be of the same type");
}
totalLength += arrays[i].Length;
}

Array ret = Array.CreateIns tance(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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Martin Honnen <ma*******@yaho o.de> wrote:
You could use an ArrayList and fill that and finally convert it to an Array:

DirectoryInfo dirInfo = new DirectoryInfo(@ "C:\whereever\w hatever");
ArrayList fileInfoList = new ArrayList(dirIn fo.GetFiles(@"* .html"));
fileInfoList.Ad dRange(dirInfo. GetFiles(@"*.as px"));
object[] fileInfos = fileInfoList.To Array();
Console.WriteLi ne(fileInfos.Le ngth);

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


Unless you use ArrayList.ToArr ay(Type) of course, and cast the
result...

--
Jon Skeet - <sk***@pobox.co m>
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*******@yaho o.de> wrote:

object[] fileInfos = fileInfoList.To Array();
but you get an object[] Array then in .NET 1.0/1.1.

Unless you use ArrayList.ToArr ay(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
4008
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 means is that every 2 seconds, I need to add 22050 bytes to an ever expanding buffer. I have no idea at the beginning how large this buffer would need to be. Now there are several possibilities to do is, as I see it: 1. Just make the buffer a...
14
1752
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 doubleMe()
4
2347
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 them in the debugger). After encoding the string (the sameone which complete value is not visible from the debugger) all the values can be seen but they are spaced by big amounts of zeros and use more that the 2048 bytes allocated. It is like if...
7
6745
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. SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ Where S is the command for SEND and should just be the character S. Where W is a byte representing how long the filename (testfile.txt) is. In this case 12. Where XXXXXXX is converted from a string that...
4
2032
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 Rosenheim
1
4759
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 if the axises were incorrectly chosen. As far as I can see concatenate((b,c),0) does it vertically while a 1 does it
21
2324
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 solve in this small code, I can solve it on my own program...you don't want to have head-ache :-) So, the problem excatly is, I built an array..just like this one.
1
2394
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 any help is very much appreciated. Regards Rolf
8
2962
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 message2 = "hello" + " world" + exclam; the first one runs fine but 2nd does not as we can not combine 2
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.