Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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_Multidimensional Array - Fatal编程技术网

如何在c#中创建锯齿状数组?

如何在c#中创建锯齿状数组?,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我熟悉C语言,我可以这样写: uint array[0xFFFF][20]; 但我不知道如何在Csharp中实现这一点。我尝试了所有MS教程和其他教程,但没有成功 有人能帮帮我吗?。基本上,您首先创建“外部”数组,然后通过它循环,然后分别创建每个“内部”数组 两个例子: int[][] a = new int[] { new int[]{ 1, 2 }, new int[]{ 3, 4, 5, 6, 7 }}; int[][] c = new int[100][]; for (int i =

我熟悉C语言,我可以这样写:

uint array[0xFFFF][20];
但我不知道如何在Csharp中实现这一点。我尝试了所有MS教程和其他教程,但没有成功

有人能帮帮我吗?

。基本上,您首先创建“外部”数组,然后通过它循环,然后分别创建每个“内部”数组

两个例子:

int[][] a = new int[] { new int[]{ 1, 2 }, new int[]{ 3, 4, 5, 6, 7 }};

int[][] c = new int[100][];
for (int i = 0; i < c.length; i++) c[i] = new int[5];
int[]]a=newint[]{newint[]{1,2},newint[]{3,4,5,6,7};
int[][]c=新int[100][];
对于(int i=0;i
如果只需要一个二维矩形数组,可以使用
int[,]b=newint[10,7]

int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];


请参见

我不知道在C中,但在C中,可以使用锯齿状数组,如:

// Declare local jagged array with 3 rows.
    int[][] jagged = new int[3][];

    // Create a new array in the jagged array, and assign it.
    jagged[0] = new int[2];
    jagged[0][0] = 1;
    jagged[0][1] = 2;

    // Set second row, initialized to zero.
    jagged[1] = new int[1];

    // Set third row, using array initializer.
    jagged[2] = new int[3] { 3, 4, 5 };

    // Print out all elements in the jagged array.
    for (int i = 0; i < jagged.Length; i++)
    {
        int[] innerArray = jagged[i];
        for (int a = 0; a < innerArray.Length; a++)
        {
        Console.Write(innerArray[a] + " ");
        }
        Console.WriteLine();
    }
//声明包含3行的本地锯齿数组。
int[][]锯齿状=新int[3][];
//在锯齿状数组中创建一个新数组,并分配它。
锯齿状[0]=新整数[2];
锯齿状[0][0]=1;
锯齿状[0][1]=2;
//设置第二行,初始化为零。
锯齿状[1]=新整数[1];
//使用数组初始值设定项设置第三行。
锯齿状[2]=新整数[3]{3,4,5};
//打印出锯齿状数组中的所有元素。
for(int i=0;i

我想这可能会对你有所帮助

       static void Main(string[] args)
    {
        Console.WriteLine("enter the size of rows");

        int n = Convert.ToInt32(Console.ReadLine());

        int[][] a = new int[n][];
        for (int i = 0; i < a.Length; i++)
        {
            Console.WriteLine("enter the number of elements for row {0}", i + 1);
            int x = Convert.ToInt32(Console.ReadLine());
            a[i]=new int[x];
            for (int j = 0; j < a[i].Length; j++)
            {
                a[i][j] = Convert.ToInt32(Console.ReadLine());
            }
        }
        for (int i = 0; i < a.Length; i++)
        {
            for (int j = 0; j < a[i].Length; j++)
            {
                Console.Write(a[i][j]+"\t"); 
            }
            Console.WriteLine();
        }

            Console.ReadKey();
    }
static void Main(字符串[]args)
{
Console.WriteLine(“输入行的大小”);
int n=Convert.ToInt32(Console.ReadLine());
int[]a=新的int[n][];
for(int i=0;i
名称空间jaggedarray
{
班级计划
{
静态void Main(字符串[]参数)
{
int[][]锯齿状=新int[5][];
锯齿状[0]=新整数[4]{22,33,44,55};
锯齿状[1]=新整数[1]{2};
锯齿状[2]=新整数[4]{1,2,3,4};
锯齿形[3]=新整数[2]{12,13};
锯齿形[4]=新整数[1]{2};
for(int i=0;i
创建和显示锯齿数组的元素

 int[][] a = new int[2][];//its mean num of row is 2 which fixed
        int choice;//thats i left on user choice that how many number of column in each row he wanna to declare

        for (int row = 0; row < a.Length; row++)
        {
            Console.WriteLine("pls enter number of colo in row {0}", row);
            choice = int.Parse(Console.ReadLine());
            a[row] = new int[choice];
            for (int col = 0; col < a[row].Length; col++)
            {
                a[row][col] = int.Parse(Console.ReadLine());
            }
        }
        //loop for out put the values of jagged array
        for (int row = 0; row < a.Length; row++)
        {
            for (int col = 0; col < a[row].Length; col++)
                Console.Write(a[row][col]+"\t");
            Console.WriteLine("");
        }
int[]a=新的int[2][]//它的平均行数是2,这是固定的
智力选择//这就是我留给用户的选择,即他想声明的每行中有多少列
for(int row=0;row
这里是我使用的插入循环

a[行].长度

而不是

a、 长度


背后的原因是每一行的列数不同,因此在第二个循环中,您必须提到每一行的长度

您的语法完全错误。您填充的唯一数组是第一个数组。是的,我已经看到了该实现。但是如果我用它,说我的内部数组有100个元素,我必须把这个句子写100遍。对吗?我刚刚遇到这样一个问题:data[,]=newuint[0xFFFF,20];看起来这样做了。当然,这就是我在和中提到的矩形2D数组。可能重复Hello和欢迎使用堆栈溢出。只有代码的答案通常不是很有用,除非它们解释了它们的功能。对于长代码段来说,这是非常正确的。请考虑对您更改或写入的内容进行解释。@ MatZi您的第一行是不正确的,亲爱的,您也可以尝试,您写新的INT[],虽然这是错误的。这是正确的内部锯齿数组INT[][] A= new INT[][] {new INT[] { 1, 2 },新INT[] { 3, 4, 5,6, 7 }};
namespace jaggedarray
{
    class Program
    {
        static void Main(string[] args)
        {

            int[][] jagged = new int[5][];
            jagged[0] = new int[4] { 22, 33,44,55 };
            jagged[1] = new int[1] { 2 };
            jagged[2] = new int[4] { 1, 2, 3, 4 };
            jagged[3] = new int[2] { 12,13};
            jagged[4] = new int[1] {2};
            for (int i = 0; i < jagged.Length; i++ ) {
                for (int j = 0; j < jagged[i].Length; j++)
                {
                    Console.Write("\t{0}",jagged[i][j]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            // input jagged array

            int [][] arrays=new int[5][];
            Console.WriteLine("Please Enter the ValueType to make a jagged array:");
            arrays[0] = new int[1];
            arrays[1] = new int[2];
            arrays[2]=new int [3];
            arrays[3]=new int [4];
            arrays[4] = new int[5];
            for (int i = 0; i < arrays.Length;i++ ) {

                for (int j = 0; j < arrays[i].Length; j++)
                {
                    arrays[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.WriteLine("This is jagged array::");
            for (int i = 0; i < arrays.Length;i++ ) {

                for (int j = 0; j < arrays[i].Length;j++ ) {
                    Console.Write("\t{0}", arrays[i][j]);

                }
                Console.WriteLine();
            }
        }

    }
}
 int[][] a = new int[2][];//its mean num of row is 2 which fixed
        int choice;//thats i left on user choice that how many number of column in each row he wanna to declare

        for (int row = 0; row < a.Length; row++)
        {
            Console.WriteLine("pls enter number of colo in row {0}", row);
            choice = int.Parse(Console.ReadLine());
            a[row] = new int[choice];
            for (int col = 0; col < a[row].Length; col++)
            {
                a[row][col] = int.Parse(Console.ReadLine());
            }
        }
        //loop for out put the values of jagged array
        for (int row = 0; row < a.Length; row++)
        {
            for (int col = 0; col < a[row].Length; col++)
                Console.Write(a[row][col]+"\t");
            Console.WriteLine("");
        }