【问题标题】:How to input and output values in array [][,] and [,][] (mix of jagged array and multidimensional array)如何在数组[][,]和[,][]中输入和输出值(交错数组和多维数组的混合)
【发布时间】:2019-03-11 19:44:43
【问题描述】:

我正在尝试改进如何在多维数组中查找项目的代码,因为我想避免在增加数据量时可能出现的未来性能问题。我是编程新手,所以有很多我不知道的东西。我一直在围绕多维数组、锯齿状数组、排序等主题进行大量搜索。我想我需要使用锯齿状数组,因为我需要排序才能找到第三大和 6.largest 的数字。但是我意识到我必须就示例寻求一些帮助或链接到更多信息,因为我在定义我的锯齿状数组时遇到了问题。我将尝试隔离每个问题,因为我陷入了我认为对于比我更熟悉数组的人来说可能很容易的事情。应该可以根据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: 我想将其更改为 [,][] 但是我遇到了如何在这种锯齿状数组中输入/输出数据的问题。如何输入数据?如何Writeline/for/foreach查看锯齿数组[,][]的内容?

【问题讨论】:

  • 这几乎总是表明缺少类,在这种情况下,您很多最好创建一个新的类类型并将实例放入一个列表。
  • 乔尔是正确的;混合参差不齐的多维矩形数组几乎总是错误的做法。一个更好的想法是在一个或多个通用类中清楚地描述您正在操作的数据的语义,然后将它们组合起来。有关为什么做你正在做的事情会令人困惑的更多想法,请参阅我 2009 年关于该主题的文章:blogs.msdn.microsoft.com/ericlippert/2009/08/17/…

标签: c# multidimensional-array jagged-arrays


【解决方案1】:

您需要遍历每个维度:

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 

【讨论】:

  • 非常感谢磁控管。这解决了我的问题!您对 [][,] 的输入也帮助我解决了 [,][] 的相同问题
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2018-08-01
    • 2019-11-26
    • 1970-01-01
    • 2012-07-25
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多