0

I want to make a string array with values of names and some numbers(which are strings) i want to pass them into a function that will take the array and then split them into an object jagged array (1 array of strings and 1 array of ints)
the array is:

string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};

and the function looks like this:

public object[][] bloop (string[] bstr)
{

}

whats next?

2
  • 9
    Next step: you show us what you've tried. Commented Mar 21, 2016 at 9:33
  • pastebin.com/DHPRuDTu Commented Mar 21, 2016 at 9:57

4 Answers 4

3

Your scenario looks like bad design that can cause errors and performance issues. The better way is to change code for using generic List<> or something like that. But in your current problem you can use below code:

public object[][] bloop (string[] bstr)
{
    var numbers = new List<int>();
    var strings = new List<string>();
    var result = new object[2][];

    foreach(var str in bstr)
    {
        int number = 0;
        if(int.TryParse(str, out number))
        {
            numbers.Add(number);
        }
        else
        {
            strings.Add(str);
        }
    }

    result[0] = strings.ToArray();
    result[1] = numbers.ToArray();

    return result;
}
Sign up to request clarification or add additional context in comments.

Comments

0
public static object[][] bloop(string[] bstr)
    {
        object[][] result = new object[2][] { new object[bstr.Length], new object[bstr.Length] };
        int sFlag = 0, iFlag = 0, val;            
        foreach (string str in bstr)
            if (int.TryParse(str, out val))
                result[1][iFlag++] = val;
            else
                result[0][sFlag++] = str;
        return result;
    }

2 Comments

It seems that you've allocated too many items with new object[bstr.Length]; that's why tail items of bloop will be nulls
Yes, you are right, we can't predict how many strings/integers will be there, we can this avoid by using Generic collections/List and Auto Increment able arrays.
0

I agree that your requirement sounds odd and should be solved with a different approach. However, this will do what you want:

public T[][] Bloop<T>(T[] items)
{
    if (items == null) throw new ArgumentNullException("items");
    if (items.Length == 1) return new T[][] { items, new T[] { } };

    int firstLength = (int) Math.Ceiling((double)items.Length / 2);
    T[] firstPart = new T[firstLength];
    Array.Copy(items, 0, firstPart, 0, firstLength);
    int secondLength = (int)Math.Floor((double)items.Length / 2);
    T[] secondPart = new T[secondLength];
    Array.Copy(items, firstLength, secondPart, 0, secondLength);
    return new T[][] { firstPart, secondPart };
}

Your sample:

string[] str= { "toto", "the", "moto", "my", "friend","12","13","14","99","88"};
string[][] result = Bloop(str);

If you need the second array as int[] you could use following:

int[] ints = Array.ConvertAll(result[1], int.Parse);

Comments

0

Linq solution.

You have two groups: first one has items that can be parsed to int and the second group contains all the others, so GroupBy looks quite naturally:

public Object[][] bloop(string[] bstr) {
  if (null == bstr)
    throw new ArgumentNullException("bstr");

  int v;

  return bstr
    .GroupBy(x => int.TryParse(x, out v))
    .OrderBy(chunk => chunk.Key) // let strings be the first
    .Select(chunk => chunk.ToArray())
    .ToArray();
}

Test:

string[] str = { "toto", "the", "moto", "my", "friend", "12", "13", "14", "99", "88" };

// toto, the, moto, my, friend
// 12, 13, 14, 99, 88
Console.Write(String.Join(Environment.NewLine, 
   bloop(str).Select(x => String.Join(", ", x))));

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.