how to copy a array in C#
Solution 1:
You can query initial array
with a help of Linq:
using System.Linq;
...
int[] array = ...
int[] positive = array.Where(item => item > 0).ToArray();
To show positive
array in one go you can try string.Join
:
Console.WriteLine(string.Join(", ", positive));
You can solve the problem with a help of loops (no Linq solution):
int[] array = ...
int positiveCounter = 0;
foreach (int item in array)
if (item > 0)
positiveCounter += 1;
int[] positive = new int[positiveCounter];
int index = 0;
for (int i = 0; i < array.Length; ++i)
if (array[i] > 0)
positive[index++] = array[i];
please note, that Array.Copy
copies consequent items which is not guaranteed.