How can we find the number of elements in an array of C # integers?

I need to find the number of elements in a C # array whose type is an integer.

I mean:

int[] intArray=new int[10] int[0]=34 int[1]=65 int[2]=98 

The number of elements for intArray is 3.

I found the code for strArray below, but it does not work for int arrays.

 string[] strArray = new string[50]; ... int result = strArray.Count(s => s != null); 
+7
source share
5 answers

Well, you must first decide what the invalid value is. Is it 0? If so, you can do this:

 int result = intArray.Count(i => i != 0); 

Note that this only works because by default the elements of the int array are initialized to zero. You will need to fill the array with another invalid value in advance, if in your situation it turns out to be 0.

Another way is to use a type with a null value:

 int?[] intArray = new int?[10]; intArray[0] = 34; intArray[1] = 65; intArray[2] = 98; int result = intArray.Count(i => i.HasValue); 
+8
source

While itsme86 provided you with a good answer to your real question , I suspect that you are better off redefining how you write this in full.

If this is your goal, I would recommend thinking about it differently. Instead of allocating an array of a fixed size and assigning certain values ​​to it, you may need to use a List<int> :

 List<int> intList = new List<int>(); intList.Add(34); intList.Add(65); intList.Add(98); 

The number of elements will always be intList.Count , and you can add as many elements as you want without worrying about the "allocated size", as the list will automatically grow as needed. It will also not give you bad results if you add 0 to the list as an actual value, where counting non-zero elements will not be considered zero if it is a valid value.

Note that you can also access elements by index, as well as an array:

 int secondValue = intList[1]; // Access like you do with arrays 
+3
source
 int[] intArray=new int[3] // Edit: Changed this to 3 to make my answer work. :) int[0]=34 int[1]=65 int[2]=98 int count = intArray.Length; // <-- Is this what you're after? 

Edit:

Um. As was humbly pointed out to me, Length will return the total number of elements in the array, which in your example would be 10. If you are looking for the number of non-zero elements in the array, you should, as suggested in some other answers.

+1
source

When you initialize an integer array without specifying any values, C # assigns a value of zero to each element . Therefore, if zero is not a valid value for your array, you can always check this.

Alternatively, you can initialize the elements of your array to a value that is not valid in your context (i.e. if negative numbers are invalid, initialize to -1), and then cycle through the array, counting the actual elements.

0
source

If it is guaranteed that the array will be accessible only sequentially, you can beat the full iterative IEnumerable counter (for large arrays) with a small gap and victory, for example

 static int DivideCount(int[] arr, int idx, int bottom, int top) { if (idx <= 0) return 0; else if (idx >= arr.Length - 1) return arr.Length; else if (arr[idx] == 0 && arr[idx - 1] != 0) return idx; else if (arr[idx] == 0 && arr[idx - 1] == 0) return DivideCount(arr, bottom + ((idx - bottom) / 2), bottom, idx); else if (arr[idx] != 0 && arr[idx - 1] != 0) return DivideCount(arr, top - ((top - idx) / 2), idx, top); else return -1; // hello compiler } int[] intArray = new int[10]; intArray[0] = 35; intArray[1] = 65; intArray[2] = 98; var count = DivideCount(intArray, intArray.Length / 2, 0, intArray.Length); 
0
source

All Articles