简体   繁体   中英

how to copy an array into a jagged array

When I try to copy arrays into a jagged array. My goal is to take an array of type char, separate the "words" into separate arrays (I use an already working function for it) and want to put them into an array.

static char[][] split_string(char[] str)
{
    int length = count_words(str);
    char[][] sentence = new char[length][];
    int start = 0;
    int end = 0;
    int word = 0;
    int i = -1;
    while (i != str.Length)
    {
     i++;
     if (str[i]==' ')
     {
      end = i-1;
      char[] aux_array = substring(str, start, end);
      //issue
      aux_array.CopyTo(sentence[word], 0);
      //alternative (not working either)
      /*
      for(int j=0; j<aux_array.Length;j++)
      {
       sentence[word][j] = aux_array[j];
      }
      */
      while (str[i]==' ')
      {
       i++;
      }
      word++;
      start = i;
     }
    }
    return sentence;
   }

For information,
substring if of the form: substring(array, int, int) -> array count_word is of the form: count_word(array) -> int

My goal is to take an array of type char, separate the "words" into separate arrays (I use an already working function for it) and want to put them into an array.

Then just put them into array

//...
sentence[word] = substring(str, start, end);

Note that the jagged array elements are null by default and you didn't allocate them, so you probably are getting null reference exception. If you really need to do a copy of the returned array, then the easiest way is to use Array.Clone method like this

sentence[word] = (char[])substring(str, start, end).Clone();

It is easier to work with strings and not raw char arrays but I assume it is with intention that you have decided to use char arrays.

One way to simplify your code is to build the char array as you go instead of preallocating it. .NET arrays have fixed size but List<T> allows you to grow a collection of items.

You can also change your function into an iterator block to simplify it further. When a word is complete you yield return it to the caller.

IEnumerable<char[]> SplitString(char[] str) {
  var word = new List<char>();
  foreach (var ch in str) {
    if (ch == ' ') {
      if (word.Count > 0) {
        yield return word.ToArray();
        word = new List<char>();
      }
    }
    else
      word.Add(ch);
  }
  if (word.Count > 0)
    yield return word.ToArray();
}

This function will not return an array so if you want an array of arrays (jagged array) you need to use ToArray() :

var str = "The quick brown fox jumps over the lazy dog".ToCharArray();
var result = SplitString(str).ToArray();

This code will correctly handle multiple spaces and spaces in the beginning and end of the source string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM