c#数组在未指定时将输出0 [英] c# array outputs 0 when it isnt specified

查看:142
本文介绍了c#数组在未指定时将输出0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出奇数和偶数的数组,我已经得到它所有的工作,但由于某种原因,当我创建一个foreach循环和输出每个数字是奇数/甚至它从零开始?

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?

设置为用户在每个字母之间输入10个数字(以逗号开头)(以字符串开头),然后删除逗号并将数字转换为一个int并将它放在一个int数组中,然后在其他类中使用。我输入1,2,3,4,5,6,7,8,9,1以测试它。

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 + " ");
    }
}

p>

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);
            }
        }

这里是RemoveDuplicates方法:

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;
        }

作为输出i get(vvv),但我不想0的开头他们,我不想要一个0在所有

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

推荐答案


但我不想在他们的开头的0,我不想要一个0在所有

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

然后你不应该创建一个包含0的数组,这是你正在做的。

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

在调用 Array.Sort(odds)之前,你的数组将拥有 NumArray ,后跟一组0值(与 NumArray 中的偶数值一样多),因为在构造数组时,会自动使用默认值元素类型。如果整个 NumArray 是奇数,则只有0值。

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.

根据需要,所以最好使用 List< int> ,只需在需要时添加即可。你可以调用 ToArray()在最后创建一个数组,如果你真的需要。

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.

这篇关于c#数组在未指定时将输出0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆