c# array outputs 0 when it isnt specified

2019-09-11 21:29发布

问题:

I am trying to output the odd and even numbers of an array, i have got it all working but for some reason when i create a foreach loop and output each number that is odd/even it starts with a zero?

The way it is setup that the user inputs 10 numbers with a comma in between each letter (Starts as a string) and then it removes the comma and converts the numbers into an int and places it in an int array, to then be used in the other class. I input 1,2,3,4,5,6,7,8,9,1 to test it.

    Odds(ai.Odds);
    Evens(ai.Evens);



    Console.ReadKey();
}
    public static void Odds(int[] odds)
    {
        Console.WriteLine("\nOdds:");
        foreach (var odd in odds)
        {
            Console.Write(odd + " ");   
        }
    }

public static void Evens(int[] evens)
{
    Console.WriteLine("\nEvens:");
    foreach (var even in evens)
    {
        Console.Write(even + " ");
    }
}

and in the separate class doing the magic:

public int[] Odds
        {
            get
            {
                int count = 0;
                int[] odds = new int[NumArray.Length];
                string[] oddNums = {"1", "3", "5", "7", "9"};
                foreach (int num in NumArray)
                {
                    foreach (string oddNum in oddNums)
                    {
                        if (num.ToString().EndsWith(oddNum))
                        {
                            odds[count++] = num;
                        }
                    }
                }
                Array.Sort(odds);
                return RemoveDuplicates(odds);
            }
        }

        public int[] Evens
        {
            get
            {
                int count = 0;
                int[] evens = new int[NumArray.Length];
                string[] evenNum = {"0", "2", "4", "6", "8"};
                foreach (int num in NumArray)
                {
                    foreach (string oddNum in evenNum)
                    {
                        if (num.ToString().EndsWith(oddNum))
                        {
                            evens[count++] = num;
                        }
                    }
                }
                Array.Sort(evens);
                return RemoveDuplicates(evens);
            }
        }

Here is the RemoveDuplicates Method:

private int[] RemoveDuplicates(int[] numbers)
        {
            if (numbers.Length == 0)
            {
                return numbers;
            }
            int[] uniques = new int[numbers.Length];

            int previousVal = numbers[0];
            uniques[0] = numbers[0];
            int count = 1;
            for (int i = 1; i < numbers.Length; i++)
            {
                if (numbers[i] == previousVal) continue;
                previousVal = numbers[i];
                uniques[count++] = numbers[i];
            }
            Array.Resize(ref uniques, count);
            return uniques;
        }

as an output i get(vvv), but i don't want the 0's at the beginning of them both, i don't want a 0 at all

回答1:

but i don't want the 0's at the beginning of them both, i don't want a 0 at all

Then you shouldn't create an array with 0s in, which is what you're doing.

Before you call Array.Sort(odds), your array will have all the odd numbers from NumArray, followed by a bunch of 0 values (as many as there are even values in NumArray), because when you construct an array, it is automatically filled with the default value of the element type for each element. You'll only have no 0 values if the whole of NumArray is odd.

You only want as many elements as you need, so it would be better to use List<int>, and just add to that when you need to. You can call ToArray() to create an array at the end, if you really need to.



回答2:

When you're assigning values to the evens and odds arrays, you're using [count++] as the index. This means that it's never assigning index 0 and it'll have a default value of 0 from when the array was initialised.



标签: c# arrays class