7

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.

2
  • 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? Commented Jul 19, 2013 at 19:08
  • 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. Commented Jul 20, 2013 at 10:49

5 Answers 5

10
string[] ASIA = new []{ Taiwan, Vietnam, Korea, China, Japan}
                      .SelectMany(s => s)  // not entirely sure what these abbreviations are...
                      .ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

For those that aren't familiar with SelectMany.
8

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();

Comments

6

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.

Comments

0

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();

1 Comment

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

You might want to create a jagged array like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.