【发布时间】: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