【问题标题】:Stack and Generic Types (jagged arrays)堆栈和泛型类型(锯齿状数组)
【发布时间】:2014-09-18 03:12:09
【问题描述】:
  • 好的,我正在制作一个 Stack whit 数组。但我需要像锯齿状数组一样在堆栈中推送数组。
  • 问题:没有有效的参数。

方法推送:

 public bool Push(T [] dato, int tamanio2)
    {
        if(tope==max)
        {
            Console.WriteLine("Imposible ingresar datos, la pila esta llena");
            return false;
        }
        else
        {
            Arreglo[tope] = dato[tamanio2];
            tope++;
            return true;
        }
    }

主要:

Cpila < string [] > pila = new Cpila< string[] >(10);

        string [] Nombres = new string [5] {"Carlos","Jose", "Patricio","Pedro","Andres"};
        pila.Push(Nombres,5);

【问题讨论】:

  • 问题是什么?
  • 我需要将 main 中的初始化数组传递给 Push 方法,但它给了我一个错误

标签: c# arrays static stack jagged-arrays


【解决方案1】:

我不完全理解这里的代码,但我可以从我对 C# 的了解中指出,当你拥有这段代码时 - Cpila &lt; string [] &gt; pila = new Cpila&lt; string[] &gt;(10); 您本质上是在说 Type 参数 T = string[]

那么你就有了public bool Push(T [] dato, int tamanio2)的方法,即替换类型参数public bool Push(string[][] dato, int tamanio2)

现在当你调用方法 push.. 像 pila.Push(Nombres,5); 这样你传递的是数字,它是 string[],而不是 string[][]

希望这可以帮助您修复代码..

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多