Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Tomas Takac 16-Nov-15 3:39am    
You use them when you need them. 2D array can be used for a matrix for example. I don't think I ever used a 3D array.
BillWoodruff 16-Nov-15 5:03am    
What is stopping you from reading the basic documentation, and the tutorials, available easily from Microsoft, and other sources ?
Hitesh Jain 16-Nov-15 5:45am    
If I Can Get Real World Example From MSDN Or From Other Site's Than I Would Not Have Posted My Question Here , I Agree With You That documentation, and the tutorials, available easily But Real World Example And Were And When To Use The Particular Concept Are Very Difficult To Find
BillWoodruff 16-Nov-15 5:58am    
The documentation and tutorials from MS do contain real-world examples. And, there are articles here on CodeProject that explain arrays, and their use, with real-world examples.

Here's the number one result from searching Google on "C# array:"

https://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

Which has many "examples."

You just need to learn to make more effort, and look more closely ... if you are ever going to really learn anything.
Hitesh Jain 16-Nov-15 6:01am    
ohhhhhhhhhhhhhh , i see

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:
C#
Piece[,] chessBoard = new Piece[8, 8];
And you would access a square by giving its x and y coordinates:
C#
chessBoard[0, 2] = new Bishop(White);


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:
C#
Car[,,] carPark = new Car[6, 5, 10];
And you would find your car on level 3, row 1, space 6:
C#
Car myCar = carPark[2, 0, 5];
 
Share this answer
 
Start by enlisting the hierarchy...

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

C#
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?

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


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.

C#
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,

C#
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>


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[^]
 
Share this answer
 
Comments
BillWoodruff 16-Nov-15 5:02am    
check the formatting of your first code examples: something looks very wrong
Afzaal Ahmad Zeeshan 16-Nov-15 5:09am    
Um, perhaps a hint may suffice. :-)
BillWoodruff 16-Nov-15 6:00am    
You are smart enough not to need any hints :)
Hitesh Jain 16-Nov-15 5:59am    
Looking For Better Answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900