CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Feb 2006
    Posts
    31

    Combining Arrays?

    (.NET Version 2.0.50727) VS2005

    Code:
    strGetFiles = Directory.GetFiles(strDirectory, strFileType[0], searchOption);
    
    strGetFiles1 = Directory.GetFiles(strDirectory, strFileType[1], searchOption);
    I am getting all the file names(locations) of a specific type and placing them into a string array (strGetFiles[]). I am having to do this 2 times (into two seperate arrays) because I am searching for *.bmp in the first one and *.gif in the second. This is forcing me to use two different arrays when I really would like all of them to be combined into one array. I would like to do something like this:

    Code:
    strGetFiles = Directory.GetFiles(strDirectory, strFileType[0], searchOption);
    
    strGetFiles += Directory.GetFiles(strDirectory, strFileType[1], searchOption);
    I know this doesn't work, but you get the idea. I want to append the second array to the end of the first one. Is there a way to do this? Or is there a way to use the GetFiles command and include more than one filetype?

    If I need to elaborate, please let me know.

    Thanks,
    Aaron

  2. #2
    Join Date
    Nov 2007
    Posts
    110

    Re: Combining Arrays?

    You could use the array copyto command and do something like...


    Code:
    strGetFiles = Directory.GetFiles(strDirectory, strFileType[0], searchOption);
    
    Directory.GetFiles(strDirectory, strFileType[1], searchOption).CopyTo(strGetFiles, strGetFiles.length);
    I don't remember the exact syntax, also you would have to make sure your strGetFiles array is large enough to hold the values of both arrays.

  3. #3
    Join Date
    Feb 2006
    Posts
    31

    Re: Combining Arrays?

    Quote Originally Posted by jshultz
    You could use the array copyto command and do something like...


    Code:
    strGetFiles = Directory.GetFiles(strDirectory, strFileType[0], searchOption);
    
    Directory.GetFiles(strDirectory, strFileType[1], searchOption).CopyTo(strGetFiles, strGetFiles.length);
    I don't remember the exact syntax, also you would have to make sure your strGetFiles array is large enough to hold the values of both arrays.
    I didn't predefine a size because I had not idea how many files would be returned. Is that a problem?

  4. #4
    Join Date
    Nov 2007
    Posts
    110

    Re: Combining Arrays?

    If you are just setting it equal to another array that shouldn't be a problem, if you want to use the CopyTo function, it might be. I haven't tested this. Give it a shot and see what happens. Worse that can happen is it throws an exception/crashes =-)

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Combining Arrays?

    use a generic List<string> data type and use AddRange to add the results of the GetFiles, then return it to an standard array using the lists ToArray method.

  6. #6
    Join Date
    Jan 2008
    Posts
    13

    Re: Combining Arrays?

    Come on guys... Are we afraid to write a little code?

    Code:
    String[] strGetFiles;
    String[] strFileType;
    
    foreach (string type in strFileType) {
        strGetFiles = appendArray(strGetFiles, Directory.GetFiles(strDirectory, type, searchOption));
    }
    
    private String[] appendArray(String[] arr1, String[] arr2) {
        String[] resultArr = new String[arr1.Length + arr2.Length];
        int i,j;
    
        for (i = 0; i < arr1.Length; i++) {
            resultArr[i] = arr1[i];
        }
        for (j = 0; j < arr2.Length; j++) {
            resultArr[i + j] = arr2[j];
        }
        
        return resultArr;
    }
    Of course, if you're lazy you can always go MadHatter's route which would probably be much easier. :-)

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Combining Arrays?

    Are we afraid to write a little code?
    No, but using a built in (such as MadHatters) is a better way.

    Custom code like you wrote will cost money over the long run.

    The odds of a bug is much higher [will you bet $100 that there is not a boundary condition that will break your code?]

    One of the metrics we use for estimating maintenance costs is the number of "if" and "for" statements. These estimates have proven to be accurate.

    Consider what happens to your code if some other programmer changes the "i++" to "++i" thinking it would make the loop "more efficient"???
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: Combining Arrays?

    I agree with the above. Why write complex code to append arrays when you can do the exact same thing using an inbuilt class? Unless there's a compelling reason not to, use the built-in stuff.

    Code:
    String[] strGetFiles;
    String[] strFileType;
    
    foreach (string type in strFileType) {
        strGetFiles = appendArray(strGetFiles, Directory.GetFiles(strDirectory, type, searchOption));
    }
    
    private String[] appendArray(String[] arr1, String[] arr2) {
        List<String> list = new List<String> (arr1);
        list.AddRange(arr2);
        return list.ToArray();
    }
    Of course, using the List<T> means you don't need the 'appendArray' function anymore, you can completely remove that method. A further bonus:

    Code:
    List<String> strGetFiles;
    String[] strFileType;
    
    foreach (string type in strFileType) {
        strGetFiles.AddRange(Directory.GetFiles(strDirectory, type, searchOption)
    }
    Isn't that much shorter and easier to maintain?
    Last edited by Mutant_Fruit; January 23rd, 2008 at 12:37 PM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Jan 2008
    Posts
    13

    Re: Combining Arrays?

    I understand the argument for maintainability and minimizing complexity, and I stated that using the built in functionality would be easier and simpler. But the point of my "are we afraid of a little code" statement was pointing out that after four responses to a question asking how to do one of the most basic programming operations, no one had said "Use a for loop!". It seems like everyone's looking for the built-in solution for even the simplest of tasks. I guess I was politely trying to suggest that we are better served by knowing how to do something as opposed to being told the correct library call. In the example, my little function is not simply a way to combine two arrays, but it is how arrays are combined. To me the latter is more useful.

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    Re: Combining Arrays?

    True, true. I get your point alright and it's a good one.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  11. #11
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Combining Arrays?

    if you don't think built in options are better in the long and short run than doing things yourself, then why use C# to begin with. I'm sure anyone could manage memory better in C++ than the framework could with .net, so IMO that argument falls flat to the ground.

    Things like this always make me think of an old college professor of mine who spent an entire class session complaining about things like stl, and object oriented programming to some extent because it could never be as efficient as asm / procedural programming.

    my thought is, why re-invent the wheel?

    why not use label / go to statements instead of built in flow controls if we're not afraid of writing a little more code?

    while I couldn't care less one way over another in this situation. .net collections will always be less efficient than rolling it yourself simply because they dynamically resize the internal array larger than what is needed, and even though that doesn't typically happen that often, or is that expensive, it still factors into the big O performance. On the other side of that argument are things that have already been said, so personally I wouldn't go ape-shite over the hand coded array manipulation as much as I would if someone used goto/labels in place of standard flow control.

    personally, I like all the nice built in features of .NET, and would use a generic list.

  12. #12
    Join Date
    Feb 2006
    Posts
    31

    Re: Combining Arrays?

    True. With that mentallity you could even say, "Why use for loops? Why don't you just write every line out one by one?". I just need the code to be written quickly so I can get on with the rest of my project.

    Anyway, thanks for the help all!

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Combining Arrays?

    Quote Originally Posted by Amerikon
    I guess I was politely trying to suggest that we are better served by knowing how to do something as opposed to being told the correct library call. In the example, my little function is not simply a way to combine two arrays, but it is how arrays are combined. To me the latter is more useful.
    I believe both ways are important: 1) to understand what's going on and 2) learning the environment well enough that you don't need to do 1).

  14. #14
    Join Date
    Jan 2008
    Posts
    13

    Re: Combining Arrays?

    I wasn't trying to say that all built-in functions are bad or that they shouldn't be used. I was just trying to point out that when it comes to something as fundamental as appending two arrays isn't more important that you know how to do it as opposed to just which built-in function to call. It's not re-inventing the wheel, it's explaining how the wheel works. As I said before, adding the arrays to a list and then calling that list's .toArray() method is "a way to combine two arrays" it is not "how two arrays are combined", which is what my example was. I don't know how the .NET List class is implemented but if you were to look at java's Vector or ArrayList classes that's pretty much the exact way they combine their arrays. If you don't learn the basics, programming ends up being a magic puzzle where you just have to find the correct ordering of library calls and then it somehow does what you want it to do. There's a lot to be gained by pulling back the curtain so you can understand why calling List.AddRange() works.

  15. #15
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Combining Arrays?

    List<T>.AddRange's only purpose in life is to combine the array passed in, with the current internal array.

    One doesn't have to understand the mechanics of a car to be able to drive one, and drive it well. similarly I don't believe it is important to understand the mechanics of the way .NET has implemented web services in order to consume one, I don't think its important to understand the hypertext transfer protocol in order to make an HttpWebRequest, or for that matter the way winsock2 communicates w/ the system to enable you to transfer data over a socket, and so on and so forth.

    its not like he was asking how to implement a genetic algorithm, or how to roll his own hashtable/map. He posted code that logically makes sense (using an append operator), IMO it was more of a question on how to append an array in .NET, and since .net arrays are not dynamic, the only logical way to do what his first approach was showing was to append an array. .NET collections are used for that, therefore, again, IMO using a dynamic array / collection was the answer they'd be looking for.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured