[英]How can I retrieve an array from a stack
I would like to implement a stack in a project am working on, haven't used stacks before, as I am a beginner. 我想在正在开发的项目中实现堆栈,因为我是一个初学者,所以以前没有使用过堆栈。 I just felt like they would do the trick.
我只是觉得他们会解决问题。 I got a general idea of how they work but am having troubles trying to retrieve an array that id have pushed into the stack, am returning
我对它们的工作方式有一个大致的了解,但是在尝试检索id已推送到堆栈中的数组时遇到了麻烦,正在返回
static void Main(string[] args)
{
int G;
int [] A=new int [2];
Stack st = new Stack();
for (G = 0; G < 5; G++)
{
A[0] = G;
A[1] = G;
st.Push(A);
}
foreach (Object obj in st)
{
Console.WriteLine(obj);
}
Console.ReadKey();
}
Instead of getting a set of arrays, I am getting System.Int32[]
- how can I return the contents of the array 我没有得到一组数组,而是得到了
System.Int32[]
-如何返回数组的内容
The problem is that you are using 问题是您正在使用
Console.WriteLine(obj);
passing the object as an argument. 将对象作为参数传递。 This method expects a string as input, so you should convert your array in a string before.
此方法需要一个字符串作为输入,因此,您应该先将数组转换为字符串。
This question may help you : int array to string 这个问题可能会对您有所帮助:将int数组转换为字符串
This should work: 这应该工作:
static void Main(string[] args)
{
int G;
int [] A=new int [2];
Stack st = new Stack();
for (G = 0; G < 5; G++)
{
A[0] = G;
A[1] = G;
st.Push(A);
}
foreach (Object obj in st)
{
Console.WriteLine(string.Join("", (int[])obj));
}
Console.ReadKey();
}
Also, you should notice that you are pushing the same array five times in the stack. 另外,您应该注意到在堆栈中将相同的数组压入了五次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.