How can I add a single Integer to an Integer Array?
if (valuesHigherThanAverage.Length == 0) valuesHigherThanAverage[valuesHigherThanAverage.Length] = arrayGetal;
else valuesHigherThanAverage[valuesHigherThanAverage.Length + 1] = arrayGetal;
I have this code and I have also tried with for or foreach loop but it doesn't worked. And I need to use an INT[] and may not use an List.
List<>then create new array which is 1 element bigger, assign old array and assign new value to the last element.ArrayListclass. It can easily be transformed to normal array when necessary.if (valuesHigherThanAverage.Length == 0)that means that the array has NO element, which also means that you cannot access the element at position0! Generally this:valuesHigherThanAverage[valuesHigherThanAverage.Length]will always lead to an IndexOutOfBounds exception. BecauseLengthis always 1 number larger than the maximum index you can use. Indexing starts at0and counting at1