Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to know the most efficient way of creating new arrays from the content of other arrays.

I have several arrays of strings of the type:

public readonly string[] Taiwan = { "TW", "TWO" };
public readonly string[] Vietnam = { "HM", "HN", "HNO" };
public readonly string[] Korea = { "KQ", "KS" };
public readonly string[] China = { "SS", "SZ" };
public readonly string[] Japan = { "T", "OS", "NG", "FU", "SP", "Q", "OJ", "JNX", "IXJ", "KAB", "JA", "JPx" };

It would be possible to create now a new array of string in a similar way to this?

public readonly string[] ASIA = { Taiwan, Vietnam, Korea, China, Japan};

That would contain all the strings included in the other arrays.

share|improve this question
2  
I think you need to be looking at a different storage mechanism. It might be time to represent this data in Sql tables. It will be easier to manage and make more intuitive sense. Have you worked with Sql yet? – Brian 2 days ago
I'm using this arrays to do some exclusions in data analysis and later on I would like to add to my tool a settings button to edit the exclusions as well as other stuff, you recommend me a small database to store setting? Or it would be better a XML file for that? Thanks for comment. – lostborion 2 days ago

5 Answers

string[] ASIA = new []{ Taiwan, Vietnam, Korea, China, Japan}
                      .SelectMany(s => s)  // not entirely sure what these abbreviations are...
                      .ToArray();
share|improve this answer
3  
For those that aren't familiar with SelectMany. – tokyovariable 2 days ago

You can take your attempt, which generates an array of arrays, and then flatten it using SelectMany back down into an array of strings.

public readonly string[] ASIA = new[] { Taiwan, Vietnam, Korea, China, Japan}
    .SelectMany(countryCode => countryCode )
    .ToArray();
share|improve this answer

you can concat two arrays.

string[] newArray = arrA.Concat(arrB).ToArray();

I also think that in your case you may probably want to consider a different type of data structure. I think a dictionary might be a good fit.

Dictionary<string, string[]> Asia = new Dictionary<string, string[]>();

The Key can be the Country, and the value can be the array for that country.

share|improve this answer

You can also use aggregate and union to accomplish this if you want only unique values in the string array

ASIA =  ( (new IEnumerable<string>[ ] { 
                Taiwan , Vietnam , Korea , China , Japan 
        } )
            .Aggregate ( ( a , b ) => a.Union ( b ) )
            .ToArray();
share|improve this answer
This is dramatically less efficient than the other approaches shown. You're re-constructing the internal HashSet of each set for each step of the aggregation. If it's important to have unique values you can simply stick a Distinct call to the end of a SelectMany and it will do the same thing, be clearer to the reader, and perform much better. Also, your cast doesn't do anything meaningful. – Servy 2 days ago

You might want to create a jagged array like this:

string[][] ASIA = new string[][] { Taiwan, Vietnam, Korea, China, Japan };
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.