C#阵列输出0时,心不是指定它 [英] c# array outputs 0 when it isnt specified

查看:94
本文介绍了C#阵列输出0时,心不是指定它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图输出数组的单双号,我已完成了所有的工作,但由于某种原因,当我创建一个foreach循环和输出的每个数是奇数/偶数它以零开始的?

事情是这样的设置,用户输入的10个号码,在每个字母之间的逗号(启动字符串),然后将其删除逗号和数字到一个int并把它在一个int数组转换,以然后在其他类中使用。我输入1,2,3,4,5,6,7,8,9,1对其进行测试。

 赔率(ai.Odds);
    埃文斯(ai.Evens);    Console.ReadKey();
}
    公共静态无效赔率(INT []赔率)
    {
        Console.WriteLine(\\ nOdds:);
        的foreach(VAR奇在赔率)
        {
            Console.Write(奇+);
        }
    }公共静态无效埃文斯(INT []找齐)
{
    Console.WriteLine(\\ nEvens:);
    的foreach(VAR甚至找齐)
    {
        Console.Write(甚至+);
    }
}

和在单独的类做的魔力:

 公众诠释[]赔率
        {
            得到
            {
                诠释计数= 0;
                INT []赔率=新INT [NumArray.Length]
                串[] oddNums = {1,3,5,7,9};
                的foreach(在NumArray INT NUM)
                {
                    的foreach(在oddNums串oddNum)
                    {
                        如果(num.ToString()的endsWith(oddNum))
                        {
                            赔率统计++] = NUM​​;
                        }
                    }
                }
                的Array.Sort(优势);
                返回RemoveDuplicates(优势);
            }
        }        公众诠释[]埃文斯
        {
            得到
            {
                诠释计数= 0;
                INT [] =唇上新INT [NumArray.Length]
                串[] evenNum = {0,2,4,6,8};
                的foreach(在NumArray INT NUM)
                {
                    的foreach(在evenNum串oddNum)
                    {
                        如果(num.ToString()的endsWith(oddNum))
                        {
                            埃文斯[统计++] = NUM​​;
                        }
                    }
                }
                的Array.Sort(双数);
                返回RemoveDuplicates(双数);
            }
        }

下面是RemoveDuplicates方法:

 私人INT [] RemoveDuplicates(INT []号)
        {
            如果(numbers.Length == 0)
            {
                返回的数字;
            }
            INT [] =唯一身份新INT [numbers.Length]            INT previousVal =号码[0];
            唯一身份[0] =号码[0];
            诠释计数= 1;
            的for(int i = 1; I< numbers.Length;我++)
            {
                如果(编号[I] == previousVal)继续;
                previousVal =号[I]
                唯一身份[统计++] =号[I]
            }
            Array.Resize(REF不重复计算);
            返回的唯一身份;
        }

作为输出我得到(VVV),但我不希望0在他们二人的开始,我不想要一个0所有


解决方案

  

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


那么你不应该建立在以0阵列,这是你在做什么。

在调用的Array.Sort(赔率),你的阵列将具有 NumArray 所有的奇数,接着是一串0值(多达甚至还有在 NumArray 值),因为当你构建一个数组,它会自动填充元素的默认值键入每个元素。你只有没有0值,如果整个 NumArray 为奇数。

根据您的需要,您只需要尽可能多的元素,所以这将是更好地使用列表< INT> ,只是添加到当您需要。您可以拨打 ToArray的()末创建一个数组,如果你真的需要。

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

解决方案

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.

这篇关于C#阵列输出0时,心不是指定它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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