multidimensional array and object array.

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
Whats the difference between multidimensional array and creating object and adding it to a 1 dimensional array or creating a structure and adding them to an array.
please tell the pros and cons of doing this.
 
First up, creating an object and creating a structure are not different things. Every instance of a structure is an object. If what you actually mean is creating an instance of a class and creating an instance of a structure then the differences between classes and structures are well documented so you should read some of that documentation.

As for whether to use a 2D array or a 1D array of objects with multiple properties, the choice is simple. A 2D array should only ever represent a matrix where every element is a peer, e.g. a 2D array to represent the children at their desks in a class room. You should NEVER use a 2D array to represent data where a "row" in the array represents a single record. In that case you should ALWAYS define a type, whether class or structure, to represent a record and then add multiple instances of that type to a 1D array or a collection.

As such, there are very, very few situations where 2D arrays are the best choice. Unfortunately, 2D arrays are often taught to beginners right after 1D arrays and the scenarios used to demonstrate their use are rarely actually suited to their use. Such exercises should be considered nothing more than practice at accessing elements in a 2D array so that their structure can become clear.
 
Back
Top