如何在数组中排除零输出?

时间:2021-08-18 07:32:06

I have this code. I would like to only display the parts of the array that are greater than 0. For example... A bunch of numbers are entered and stored as a variable. But not all of the variables in the array will be used. The ones that aren't are stored as 0. So when I display the array, the numbers are displayed as: "140, 180, 298, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " etc. I don't want the zeros to be displayed.

我有这个代码。我想只显示大于0的数组部分。例如......输入一堆数字并存储为变量。但并非所有数组中的变量都会被使用。那些没有存储为0.所以当我显示数组时,数字显示为:“140,180,298,130,0,0,0,0,0,0,0,0,0 ,0,0,“等我不希望显示零。

int[] scores = {score1, score2, score3, score4, score5, score6, score7, score8, score9, score10, score11, score12, score13, score14, score15};

Console.WriteLine("Your scores as you entered them: " + (string.Join(", ", scores)));
Console.ReadKey();

3 个解决方案

#1


8  

Use linq's Where:

使用linq的位置:

string.Join(", ", scores.Where(x => x != 0))

In the description above you also said the parts of the array that are greater than 0. So if that is the case you can change it to:

在上面的描述中,您还说过数组中大于0的部分。因此,如果是这种情况,您可以将其更改为:

string.Join(", ", scores.Where(x => x > 0))

For a non linq solution use a List<int> and a simple foreach (or for) loop:

对于非linq解决方案,使用List 和一个简单的foreach(或for)循环:

List<int> result = new List<int>();
foreach(int item in scores)
{
    if(item != 0)
        result.Add(item);
}

#2


1  

If you don't understand LINQ yet, you can look into this piece of code:

如果您还不了解LINQ,可以查看以下代码:

int[] scores = { 1, 2, 3, 0, 0, 4, 5, 0, 0, 6};

int[] withoutZeros = WithoutZeros (scores);

And the core method:

而核心方法:

public static int[] WithoutZeros (int[] input)
{
    int zerosCount = 0; // we need to count how many zeros are in the input array

    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] == 0) zerosCount = zerosCount + 1;
    }

    // now we know number of zeros, so we can create output array
    // which will be smaller than then input array (or not, if we don't have any zeros in the input array)

    int[] output = new int[input.Length - zerosCount]; // can be smaller, equal, or even empty

    // no we need to populate non-zero values from the input array to the output array

    int indexInOutputArray = 0;

    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] != 0)
        {
            output[indexInOutputArray] = input[i];

            indexInOutputArray = indexInOutputArray + 1;
        }
    }

    return output;
}

#3


0  

To get the answer of your question have a look at Gilad Green's answer.

为了得到你的问题的答案,看看吉拉德格林的答案。

I just wanted to give you a little feedback about the code I see.

我只想给你一些关于我看到的代码的反馈。

Since, I dont see all your code I am making a lot of assumptions here, so sorry if I am wrong.

既然,我没有看到你的所有代码,我在这里做了很多假设,如果我错了,很抱歉。

If you want to fill an array with 15 values, you should consider refactoring your code to use a loop.

如果要使用15个值填充数组,则应考虑重构代码以使用循环。

In the following example I will use a for loop but it could be solved with different loops.

在下面的例子中,我将使用for循环但它可以用不同的循环来解决。

int[] scores = new int[15]; //initialize an array of ints with a length of 15
for (int i = 0; i < scores.Length; i++) //loop from 0 till smaller than the length of the array (0-14 = 15 iterations)
    scores[i] = int.Parse(Console.ReadLine()); //fill the scores array with the index of i with the console input

#1


8  

Use linq's Where:

使用linq的位置:

string.Join(", ", scores.Where(x => x != 0))

In the description above you also said the parts of the array that are greater than 0. So if that is the case you can change it to:

在上面的描述中,您还说过数组中大于0的部分。因此,如果是这种情况,您可以将其更改为:

string.Join(", ", scores.Where(x => x > 0))

For a non linq solution use a List<int> and a simple foreach (or for) loop:

对于非linq解决方案,使用List 和一个简单的foreach(或for)循环:

List<int> result = new List<int>();
foreach(int item in scores)
{
    if(item != 0)
        result.Add(item);
}

#2


1  

If you don't understand LINQ yet, you can look into this piece of code:

如果您还不了解LINQ,可以查看以下代码:

int[] scores = { 1, 2, 3, 0, 0, 4, 5, 0, 0, 6};

int[] withoutZeros = WithoutZeros (scores);

And the core method:

而核心方法:

public static int[] WithoutZeros (int[] input)
{
    int zerosCount = 0; // we need to count how many zeros are in the input array

    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] == 0) zerosCount = zerosCount + 1;
    }

    // now we know number of zeros, so we can create output array
    // which will be smaller than then input array (or not, if we don't have any zeros in the input array)

    int[] output = new int[input.Length - zerosCount]; // can be smaller, equal, or even empty

    // no we need to populate non-zero values from the input array to the output array

    int indexInOutputArray = 0;

    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] != 0)
        {
            output[indexInOutputArray] = input[i];

            indexInOutputArray = indexInOutputArray + 1;
        }
    }

    return output;
}

#3


0  

To get the answer of your question have a look at Gilad Green's answer.

为了得到你的问题的答案,看看吉拉德格林的答案。

I just wanted to give you a little feedback about the code I see.

我只想给你一些关于我看到的代码的反馈。

Since, I dont see all your code I am making a lot of assumptions here, so sorry if I am wrong.

既然,我没有看到你的所有代码,我在这里做了很多假设,如果我错了,很抱歉。

If you want to fill an array with 15 values, you should consider refactoring your code to use a loop.

如果要使用15个值填充数组,则应考虑重构代码以使用循环。

In the following example I will use a for loop but it could be solved with different loops.

在下面的例子中,我将使用for循环但它可以用不同的循环来解决。

int[] scores = new int[15]; //initialize an array of ints with a length of 15
for (int i = 0; i < scores.Length; i++) //loop from 0 till smaller than the length of the array (0-14 = 15 iterations)
    scores[i] = int.Parse(Console.ReadLine()); //fill the scores array with the index of i with the console input