如何在数组 [][] 和 [][] 中输入和输出值(交错数组和多维数组的混合)
How to input and output values in array [][,] and [,][] (mix of jagged array and multidimensional array)
我正在尝试改进有关如何在多维数组中查找项目的代码,因为我想避免在增加数据量时可能出现的未来性能问题。我是编程新手,所以有很多东西我不知道。我一直在围绕多维数组、锯齿状数组、排序等主题进行大量搜索。我想我需要使用交错数组,因为我需要排序才能找到第三大和 6.largest 数字。但我意识到我必须寻求有关示例的一些帮助或 link 以获取更多信息,因为我在定义锯齿状数组方面遇到了问题。我将尝试隔离每个问题,因为我被困在我认为对于比我更熟悉数组的人来说可能很容易的事情上。应该可以根据 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
问题一:
如何编写 WriteLine / for / foreach 以查看锯齿状数组 [][] 的内容?
问题二:
我想将其更改为 [][] 但后来我遇到了如何在这种锯齿状数组中 input/output 数据的问题。如何输入数据?如何Writeline / for / forearch查看锯齿状数组[][]的内容?
您需要遍历每个维度:
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
如果其他读者有同样的问题,我想添加代码示例,说明 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
我正在尝试改进有关如何在多维数组中查找项目的代码,因为我想避免在增加数据量时可能出现的未来性能问题。我是编程新手,所以有很多东西我不知道。我一直在围绕多维数组、锯齿状数组、排序等主题进行大量搜索。我想我需要使用交错数组,因为我需要排序才能找到第三大和 6.largest 数字。但我意识到我必须寻求有关示例的一些帮助或 link 以获取更多信息,因为我在定义锯齿状数组方面遇到了问题。我将尝试隔离每个问题,因为我被困在我认为对于比我更熟悉数组的人来说可能很容易的事情上。应该可以根据 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
问题一: 如何编写 WriteLine / for / foreach 以查看锯齿状数组 [][] 的内容?
问题二: 我想将其更改为 [][] 但后来我遇到了如何在这种锯齿状数组中 input/output 数据的问题。如何输入数据?如何Writeline / for / forearch查看锯齿状数组[][]的内容?
您需要遍历每个维度:
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
如果其他读者有同样的问题,我想添加代码示例,说明 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