什么是2D和3D阵列的最佳使用? [英] What Is The Best Use Of 2D And 3D Array ?

查看:61
本文介绍了什么是2D和3D阵列的最佳使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是2D和3D阵列的最佳使用,我很困惑在哪里使用2D和3D阵列,所以如果有人用基本示例解释我会更好,如果需要更多信息请注释

What Is The Best Use Of 2D And 3D Array , I Am Confused That Where To Use 2D And 3D Array , So It Will Be Better That If Some One Explain Me With Basic Example , If Needed More Info Than Please Comment It

推荐答案

2D数组是一组值,它们有两个索引可以访问它:想象一下你有一个8 x 8方块的棋盘四处移动,每个方块用行号和一个列字符(小写以防止与片段标识符混淆): Chess Board [ ^ ]这是一个自然的2D阵列:

A 2D array is a group of values that have two indexes to access it: think of a chess board where you have an 8 x 8 set of squares that the pieces move around on, with each square addressed by a row number, and a column character (lower case to prevent confusion with piece identifiers): Chess Board[^] This is a "natural" 2D array:
Piece[,] chessBoard = new Piece[8, 8];

你可以通过给出x和y来访问一个正方形坐标:

And you would access a square by giving its x and y coordinates:

chessBoard[0, 2] = new Bishop(White);





3D阵列添加一个额外的维度并以指数方式增加空间量 - 实际上它是一组2D数组,每个元素由三个索引寻址,两个用于每个棋盘内的[x,y]位置,第三个用于选择棋盘。 />
在现实世界中,这可以看作是一个多层停车场:每个级别是相同的,有一个停车位的网格,整个事情是一组水平所以如果你每个级别有5行空格,每行有10个空格,共有6个级别:



A 3D array adds an extra dimension and exponentially increases the amount of space - effectively it's a set of 2D arrays, with each element addressed by three indexes, two for the [x, y] position within each chessboard, and a third to select the chessboard.
In the real world, this could be seen as a multi-story car park: each level is the same, with a "grid" of parking spaces, and the whole thing is a set of levels So if you have 5 rows of spaces on each level, and each row has 10 spaces, and there are 6 levels:

Car[,,] carPark = new Car[6, 5, 10];

你会发现你的车在3级,1行,6号空间:

And you would find your car on level 3, row 1, space 6:

Car myCar = carPark[2, 0, 5];


首先征集层次结构...



首先,有一个简单的对象。您可以将它用于任何任务。如下所示,



Start by enlisting the hierarchy...

First of all, there is a simple object. You can use it for any task. Like the one below,

object obj = new Object();





然后,有一个列表,一个数组。它有什么作用?它中包含许多对象,对吧?如下所示?





Then, there is a list of them, an array. What does it do? It holds a number of objects in it, right? Like the one below?

object[] objArray = { new Object(), "Strings allowed", 1234, 53.78 };





这更像是一个数组,你可能想要存储一些价值观。那是数组的用户。像学生一样,获得的标记数组等等。那么,您想在哪里使用2D阵列? 2D数组用于存储对象数组的数组。





That is more like an array, where you might want to store a number of values. That is the user of arrays. Like the array of students, array of marks obtained and so on. So, where would you want to use a 2D array? A 2D array is used to store an array of array of objects.

object[,] objArrayofArray = 
{
   { "Afzaal Ahmad Zeeshan", 20, 5.9 },
   { "Daniyal Ahmad Rizwan", 15, 5.4 },
   { "John Doe", 50, 6.0 }
};





用于什么?保存数组数组。该数组只是结构,用于保存用户的姓名,年龄和身高。然后你可以访问这些值,





What is that used for? To hold an array of array. The array is simply of the structure, to hold the name, age and height of the users. You can then access the values, like this,

class User {
   public string Name { get; set; }
   public int Age { get; set; }
   public double Height { get; set; }
}

var users = new List<user>();
// Iterate over each of the item and select values.
</user>





但是你在哪里使用3D阵列是完全没有意义的。这比对你有用更痛苦。所以,在我看来,你应该考虑保留固定大小的数组,并考虑使用泛型 列表<> [ ^ ]对象。您可以创建列表列表等。它还为您提供了良好的性能,您可以跳过创建新数组来存储更多对象,因为它可以增大。



参考文章了解更多相关信息:

C#中的多维数组 [ ^ ]

C\C ++ \ C#\ Java 中的动态三维数组[ ^ ]



But where would you use 3D arrays is totally pointless. It would be just more painful, than being useful to you. So, in my opinion, you should consider leaving the arrays of fixed size and consider using the generic List<>[^] objects. You can create List of List and so on. It also provides you with good performance and you can skip creating new arrays to store more objects as it can grow in size.

Reference articles for more on this:
Multidimenstional Arrays in C#[^]
Dynamic Three Dimensional Arrays in C\C++\C#\Java[^]


这篇关于什么是2D和3D阵列的最佳使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆