0

Okay, so this seems simple, but I can't think of a straightforward solution; Basically I have an object array in C# that contains, say, 102 elements. I then also have 4 other empty arrays. I want to iterate through the original array and distribute the 100 elements evenly, then distribute 101 and 102 to the 1st and 2nd new arrays respectively.

int i = 1,a=0, b=0, c=0, d = 0;

foreach (ReviewStatus data in routingData)
{
    if (i == 1)
    {
        threadOneWork[a] = data;
        a++;
    }
    if (i == 2)
    {
        threadTwoWork[b] = data;
        b++;
    }
    if (i == 3)
    {
        threadThreeWork[c] = data;
        c++;
    }
    if (i == 4)
    {
        threadFourWork[d] = data;
        d++;
        i = 0;
    }
    i++;

}

Now the above code definitely works, but I was curious, does anybody know of a better way to do this??

3 Answers 3

5
var workArrays = new[] { 
    threadOneWork,
    threadTwoWork,
    threadThreeWork,
    threadFourWork, 
};

for(int i=0; i<routingData.Length; i++) {
    workArrays[i%4][i/4] = routingData[i];
}
Sign up to request clarification or add additional context in comments.

5 Comments

That's actually kinda sexy ;)
math can make a lot of sense sometimes.
@msarchet - I wouldn't go so far as to say "sexy" but I would say "moderately attractive"...
@ChaosPandion it just so obviously simple. and there's no LINQ which is always fun.
haha thanks for all the suggestions guys, this is definitely going to be used instead of my original post. LINQ was going to be my next step, but if i don't need it, even better!
2

Put the four arrays into an array of arrays, and use i%4 as an index. Assuming that thread###Work arrays have enough space to store the data, you can do this:

var tw = new[] {threadOneWork, threadTwoWork, threadThreeWork, threadFourWork};
var i = 0;
foreach (ReviewStatus data in routingData) {
    tw[i%4][i/tw.Length] = data;
    i++;
}

2 Comments

Are we correct in assuming you mean a multidimensional array [,] as opposed to a jagged array [][]?
@codesparkle I wrote "array of arrays" rather than "multidimensional array" to make this distinction. I just added a code example to clarify.
0

Linq is your friend! Use modulo to group the items via the total number of arrays in your case 4.

For example the code splits them up into four different lists:

var Items = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Items.Select( ( i, index ) => new {
                                      category = index % 4,
                                      value = i
                                  } )
        .GroupBy( itm => itm.category, itm => itm.value )
        .ToList()
        .ForEach( gr => Console.WriteLine("Group {0} : {1}", gr.Key, string.Join(",", gr)));
/* output
Group 0 : 1,5,9
Group 1 : 2,6,10
Group 2 : 3,7
Group 3 : 4,8
*/

1 Comment

One note, one doesn't have to create arrays ahead of time. The foreach could be used to do the processing you seek. More compact IMHO; but glad you appreciated it. :-)

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.