Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于创建锯齿阵列的C#函数_C#_Arrays_For Loop_Jagged Arrays - Fatal编程技术网

用于创建锯齿阵列的C#函数

用于创建锯齿阵列的C#函数,c#,arrays,for-loop,jagged-arrays,C#,Arrays,For Loop,Jagged Arrays,我有一个问题,我想创建一个锯齿状数组。但是这个锯齿状数组的大小是可变的。 这与填充这样的普通数组的方式相同吗 int[] terms = new int[400]; for (int runs = 0; runs < 400; runs++) { terms[runs] = value; } 根据用户所需的大小,需要根据所需的大小填充一个新的变量NodeCasette。数组的填充顺序始终相同,从0开始,到4结束 这应该是这样的,但我不知道如何动态创建它: public strin

我有一个问题,我想创建一个锯齿状数组。但是这个锯齿状数组的大小是可变的。 这与填充这样的普通数组的方式相同吗

int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
    terms[runs] = value;
}
根据用户所需的大小,需要根据所需的大小填充一个新的变量NodeCasette。数组的填充顺序始终相同,从0开始,到4结束

这应该是这样的,但我不知道如何动态创建它:

public string[][] nodeCassette =
{ 
    nodeCassette0,
    nodeCassette1,
    nodeCassette2,
    nodeCassette3,
    nodeCassette4
};

下面是如何创建这种锯齿状数组的示例:

int[] sizes = new int[] { 3, 2, 1, 1, 3 };

int[][] jagged = new int[sizes.Length][];

for (int i = 0; i < sizes.Length; i++)
{
    jagged[i] = new int[sizes[i]];
    for (int j = 0; j < sizes[i]; j++)
    {
        jagged[i][j] = (i + 1) * (j + 1);
    }
}
int[]size=newint[]{3,2,1,1,3};
int[][]锯齿状=新的int[size.Length][];
对于(int i=0;i
这将产生:


字符串也一样:

int[] sizes = new int[] { 3, 2, 1, 1, 3 };

string[][] jagged = new string[sizes.Length][];

for (int i = 0; i < sizes.Length; i++)
{
    jagged[i] = new string[sizes[i]];
    for (int j = 0; j < sizes[i]; j++)
    {
        jagged[i][j] = $"{i}_{j}";
    }
}
int[]size=newint[]{3,2,1,1,3};
字符串[][]锯齿状=新字符串[大小.长度][];
对于(int i=0;i
我们可以使用反射和Linq

using System.Reflection;
using System.Linq;
默认情况下,使用此专用类存储阵列,初始化可能元素的完整列表,并提供获取切片的方法,即由前n个元素组成的列表:

static class DefaultArrays
{
  static public string[] NodeCassette0 = { "03", "08" };
  static public string[] NodeCassette1 = { "04", "09" };
  static public string[] NodeCassette2 = { "05", "10" };
  static public string[] NodeCassette3 = { "06", "11" };
  static public string[] NodeCassette4 = { "07", "12" };

  static public List<string[]> All { get; private set; }

  static public List<string[]> Take(int count)
  {
    return All.Take(count).ToList();
  }

  static public void Initialize()
  {
    All = typeof(DefaultArrays).GetFields()
                               .Where(field => field.FieldType == typeof(string[]))
                               .OrderBy(field => field.Name.Length)
                               .ThenBy(field => field.Name)
                               .Select(field => (string[])field.GetValue(null))
                               .ToList();
  }

  static DefaultArrays()
  {
    Initialize();
  }
}
否则,我们可以使用自定义比较器:

试验

输出

0308
04, 09
05, 10
警告

该切片包含指向原始数组的引用列表,因此该列表中数组单元格的任何修改都将反映在静态类中的匹配数组中

否则我们需要使用
数组复制数组。复制
而不是使用
Linq。获取

  static public List<string[]> TakeCopy(int count)
  {
    var list = new List<string[]>();
    foreach ( int index in Enumerable.Range(0, Math.Min(count, All.Count)))
    {
      int length = All[index].Length;
      var array = new string[length];
      Array.Copy(All[index], array, length);
      list.Add(array);
    }
    return list;
  }
静态公共列表副本(整数计数)
{
var list=新列表();
foreach(Enumerable.Range(0,Math.Min(count,All.count))中的int索引)
{
int length=All[index].length;
变量数组=新字符串[长度];
复制(所有[索引]、数组、长度);
添加(数组);
}
退货清单;
}

您应该使用
List
而不是
int[]
这样您就不需要指定大小这是一个简单的解决方案,但是如何创建函数来填充此列表?是的,它的工作方式与填充普通数组相同。
var terms=new List()和循环中的
术语。添加(值)
@OlivierRogier我正在从windows窗体读取值,因此第一部分几乎是正确的。然而,据我所知,你评论中的其余部分完全正确。我理解这是如何处理int的,但这如何处理字符串数据?@Zjwamerjong-这与字符串没有什么不同。我已经更新了答案。
"[same_same][number_without_trailing_left_0]"
var slice = DefaultArrays.Take(3);

string msg = string.Join(Environment.NewLine, slice.Select(item => string.Join(", ", item)));

Console.WriteLine(msg);
  static public List<string[]> TakeCopy(int count)
  {
    var list = new List<string[]>();
    foreach ( int index in Enumerable.Range(0, Math.Min(count, All.Count)))
    {
      int length = All[index].Length;
      var array = new string[length];
      Array.Copy(All[index], array, length);
      list.Add(array);
    }
    return list;
  }