0
\$\begingroup\$

I wrote a solution Please look at the my approach is correct? There is a better solution?

List<Tuple<object, object, object>> OneDimensionalLists = new List<Tuple<object, object, object>>();
List<Tuple<object, object, object>[]> TwoDimensionalLists = new List<Tuple<object, object, object>[]>();

int i1 = 0;
for (int i = 0; i < OneDimensionalLists.Count; i++)
{
    var random = new Random(seed);
    int next = random.Next(10, 30);
    TwoDimensionalLists.Add(new Tuple<object, object, object>[next]);
    for (int j = 0; j < next; j++)
    {
        if (OneDimensionalLists.Count == i1)
            goto go;

        TwoDimensionalLists[i][j] = OneDimensionalLists[i1++];
    }
}
go:
\$\endgroup\$

closed as off-topic by t3chb0t, syb0rg, RobH, konijn, Heslacher Jul 27 '16 at 14:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – t3chb0t, syb0rg, RobH
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ I wrote a solution - well, this isn't a good description of your code and thegoto this is a no-go ;-D \$\endgroup\$ – t3chb0t Jul 27 '16 at 11:51
  • 1
    \$\begingroup\$ Could kindly explain what you are trying to achieve with this code e.g the output, input etc? \$\endgroup\$ – Siobhan Jul 27 '16 at 12:11
5
\$\begingroup\$
for (int i = 0; i < OneDimensionalLists.Count; i++)
{
    var random = new Random(seed);
    int next = random.Next(10, 30);
    ...

Have you checked the value of next on the various iterations? I can't see seed changing anywhere in this code. Assuming it remains the same, random.Next() will return the same value each time. Unless this is intentional, create the Random(seed) outside of the loop.

\$\endgroup\$
1
\$\begingroup\$

Whenever you declare and assign a variable in a single statement, if you are not going to assign it as null, use the var keyword instead of the type name.

List<Tuple<object, object, object>> oneDimensionalList = new List<Tuple<object, object, object>>();

could become

var oneDimensionalList = new List<Tuple<object, object, object>>();

which is far simpler.

The odds that someone will be randomly working with Tuple<object, object, object> are pretty slim in my opinion, so you should perhaps consider changing your method(?) to accept a List<T> if not a ICollection<T> or even an IEnumerable<T> instead.

Speaking of "method," it seems that this code should be one. It effectively takes an input and produces an output, so why not isolate it to be its own piece of code that you can call wherever you want? You could also get rid of that goto statement in favor of a proper return.

It would look something like this:

public static List<Tuple<object, object, object>[]> MakeTwoDimensional(
    List<Tuple<object, object, object>> oneDimensionalList)
{
    var twoDimensionalList = new List<Tuple<object, object, object>[]>();

    // Somehow transform 'oneDimensionalList` into `twoDimensionalList`

    return twoDimensionalList;
}

Your conditional statement using the i1 variable reads very oddly. Granted, I normally wouldn't be using a loop over a collection with a randomly generated number as the conditional, but maybe that's just me.

Outside of the code itself, I don't understand what "problem" you are trying to solve. I don't see how this type of transformation would be useful. In your example, the code will actually do nothing, as the first list is initialized empty and nothing is added to it.

\$\endgroup\$

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