如何在array [] [,]和[,] []中输入和输出值(混合了锯齿状数组和多维数组)

问题描述 投票:-1回答:2

我正在尝试改进关于如何在多维数组中查找项目的代码,因为我希望在增加数据量时避免可能的未来性能问题。我是编程的新手,所以有很多我不知道的东西。我一直在搜索主题多维数组,锯齿状数组,排序。我想我需要使用锯齿状数组,因为我需要排序才能找到第三大和最大数字。但我意识到我必须在示例上寻求一些帮助或链接到更多信息,因为我在定义我的锯齿状数组方面遇到了问题。我将尝试隔离每个问题,因为我遇到了我认为对于比我更熟悉阵列的人来说可能很容易的事情。根据jagged-arrays,应该可以混合锯齿状和多维数组

这是[] []的例子

using System;
using System.Collections;
namespace SortJaggedArray
{
class host
{
    [STAThread]
    static void Main(string[] args)
    {
        int[][] arr = new int[2][];
        arr[0] = new int[3] {1,5,3};
        arr[1] = new int[4] {4,2,8,6};

        // Write out a header for the output.
        Console.WriteLine("Array - Unsorted\n");

        for (int i = 0; i < arr.Length; i++)
        {
             System.Console.WriteLine("Outer array " + i);

             for (int j = 0; j < arr[i].Length; j++)
             {
                  System.Console.Write(arr[i][j] + " ");
             }
             System.Console.WriteLine(" ");
             System.Console.WriteLine(" ");
        }
        Console.ReadLine();
    }
}
}

//Output:
//Outer array 0
//1 5 3

//Outer array 1
//4 2 8 6

这是我的[] [,]的示例,其中输入正在工作,但我在如何编写输出方面很困难:

using System;
using System.Collections;
namespace SortJaggedArray
{
    class host
    {
        [STAThread]
        static void Main(string[] args)
        {
            int[][,] arr = new int[2][,]
            {
                new int[,] { { 1, 3 }, { 5, 2 }, { 3, 9 } },
                new int[,] { { 4, 1 }, { 2, 7 }, { 8, 5 }, { 6, 3 } }
            };
            // Write out a header for the output.
            Console.WriteLine("Array - Unsorted\n");

            foreach (int i in arr)
                Console.WriteLine(i);

            Console.ReadLine();
        }
    }
}

Wanted output:
Nr 0: 
1, 3
5, 2
3, 9

Nr 1:
4, 1
2, 7
8, 5
6, 3

问题1:如何编写WriteLine / for / foreach以查看锯齿状数组[] [,]的内容?

问题2:我想将其更改为[,] []但是后来我遇到了如何在这种锯齿状数组中输入/输出数据的问题。如何输入数据?如何写线/ for / forearch来查看锯齿状数组[,] []的内容?

c# multidimensional-array jagged-arrays
2个回答
0
投票

您需要迭代每个维度:

for(int i=0; i<arr.Length; i++){
    Console.WriteLine($"Nr {i}:");
    for(int j=0;j<arr[i].GetLength(0);j++){
        for(int k=0;k<arr[i].GetLength(1);k++){
            Console.Write($"{arr[i][j,k]} ");
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}

输出:

Nr 0:
1 3 
5 2 
3 9 

Nr 1:
4 1 
2 7 
8 5 
6 3 

-1
投票

如果其他读者有相同的问题,我想添加代码示例,说明Magnetron的示例如何帮助我解决数组[,] []的相同问题

using System;
using System.Collections;
namespace SortJaggedArray
{
    class host
    {
        [STAThread]
        static void Main(string[] args)
        {
            int[,][] arr = new int[2,3][];
            arr[0,0] = new int[3] { 1, 5, 3 };
            arr[0,1] = new int[4] { 4, 2, 8, 6 };
            arr[0,2] = new int[2] { 2, 8 };
            arr[1,0] = new int[2] { 7, 5 };
            arr[1,1] = new int[5] { 8, 7, 5, 9, 2 };
            arr[1,2] = new int[2] { 1, 4};

            // Write out a header for the output.
            Console.WriteLine("Array - Unsorted\n");

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                Console.WriteLine($"Nr {i}:");
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    for (int k = 0; k < arr[i,j].Length; k++)
                    {
                        Console.Write($"{arr[i,j][k]} ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

//Output:
//Nr 0:
//1 5 3
//4 2 8 6
//2 8

//Nr 1:
//7 5
//8 7 5 9 2
//1 4
© www.soinside.com 2019 - 2024. All rights reserved.