简体   繁体   English

将 double[] 添加到 C# 中的自定义堆栈

[英]Add double[] to to custom stack in C#

My problem is that I cannot push a double[] to my custom stack.我的问题是我无法将 double[] 推送到我的自定义堆栈。

if (int.TryParse(item[0], out intTemp))
{
   customStack.Push(parts.Skip(1).Select(int.Parse).ToArray());
}
else if(double.TryParse(item[0], out doubleTemp))
{
customStack.Push(parts.Skip(1).Select(double.Parse).ToArray());
}

The first if statement works, but the second gives me an exception - CS1503: Argument 1: cannot convert from 'double[]' to 'int'.第一个 if 语句有效,但第二个给了我一个例外 - CS1503: Argument 1: cannot convert from 'double[]' to 'int'。

The problem is I'm not even trying to add a 'double[]' into 'int'.问题是我什至没有尝试将'double []'添加到'int'中。

Here is my Push method :这是我的推送方法

public void Push(params T[] items)
    {
        foreach (T item in items)
        {
            this.stackData.Add(item);
            Console.WriteLine($"Element {item} added to stack.");
        }
    }

Help will be appreciated.帮助将不胜感激。

customStack seems to be a CustomStack<int> . customStack似乎是一个CustomStack<int> So your Push method expects a params int[] argument.因此,您的Push方法需要一个params int[]参数。

parts.Skip(1).Select(double.Parse).ToArray() returns a double[] that the compiler is now trying to convert into the first element of the int[] items parameter, which leads to your error message. parts.Skip(1).Select(double.Parse).ToArray()返回一个double[] ,编译器现在正尝试将其转换为int[] items参数的第一个元素,这会导致您的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM