Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the size of a 2D array? [duplicate]

If I declare this array...

string[,] a = {                   {"0", "1", "2"},                   {"0", "1", "2"},                   {"0", "1", "2"},                   {"0", "1", "2"},               }; 

Then I can measure the length with

a.Length 

which is 12. How do I measure the dimension of the arrays within? If I try...

a[0].Length 

I get Wrong number of indices inside []; expected 2. What gives?

like image 859
izb Avatar asked Nov 05 '10 13:11

izb


People also ask

How do you check for duplicates in a 2D array?

Turn the 2d array into a 1d array ( List<Integer> ), then loop through the 1d array counting the duplicates as you find them and removing them so you don't count them more than once.

How do you determine the size of a 2D array?

We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.

How do you check if each element in a 2D array is unique?

Put all the numbers in a Set and just match the size of array and set. If both are equal then all your numbers in array are unique.


1 Answers

You want the GetLength() method of your array:

a.GetLength(0); 

http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

like image 75
GendoIkari Avatar answered Oct 13 '22 05:10

GendoIkari