简体   繁体   English

如何将数组复制到锯齿状数组中

[英]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. 我的目标是采用char类型的数组,将“单词”分隔为单独的数组(我使用了已经起作用的函数)并将其放入数组中。

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 子字符串,如果形式为:substring(array,int,int)->数组count_word的形式为: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. 我的目标是采用char类型的数组,将“单词”分隔为单独的数组(我使用了已经起作用的函数)并将其放入数组中。

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. 请注意,锯齿状数组元素默认情况下为null ,并且您没有分配它们,因此您可能会得到空引用异常。 If you really need to do a copy of the returned array, then the easiest way is to use Array.Clone method like this 如果您确实需要复制返回的数组,那么最简单的方法是使用Array.Clone方法,如下所示

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. 使用字符串而不是原始char数组更容易,但是我认为您决定使用char数组是有意的。

One way to simplify your code is to build the char array as you go instead of preallocating it. 一种简化代码的方法是在运行时构建char数组,而不是对其进行预分配。 .NET arrays have fixed size but List<T> allows you to grow a collection of items. .NET数组具有固定的大小,但是List<T>允许您增长项目的集合。

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. 完成一个单词后,您就将其yield return给呼叫者。

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() : 此函数不会返回数组,因此,如果要数组数组(锯齿状数组),则需要使用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. 此代码将正确处理源字符串开头和结尾的多个空格和空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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