如何在C#中基于构造函数方法创建数组? [英] How can i create an array based on constructor method in c#?
问题描述
所以我一直在为c#进行最后的项目我需要创建3个数组,其中一个数组用于汽车一个用于StackArray还有一个用于QueueArray.仅基于简单数组(不使用堆栈.代码)我必须给7辆红色和蓝色之间颜色随机的汽车.红色为stackarray.蓝色表示队列数组.
So I've been working on my final project for c# And i need to create 3 classes with arrays one for cars One for StackArray And one for QueueArray. Only based on simple arrays (without usuing stack. Code) I have to give 7 cars with random colors beetween red and blue. Red for stackarray. And blue for queue array.
所以我是这个的新手...
So I'm extra newbie to this...
汽车课很简单,所以我已经完成了...现在用于堆栈和queque课...我创建了堆栈类,现在我需要使用汽车构造函数方法创建一个数组并将其命名为堆栈数组...然后我需要使用next将我的第一个值赋予堆栈类的堆栈方法中的stackarray ...
The car class is easy so I've done it... now for stack and queque class... I created the stack class and now i need to create an array using my car constructor method and name it as a Stack array... Then i need to use next to give my first value to the stackarray in stack method of the stack class...
我想我解释得很厉害,但请帮忙T-T这是我需要为每个类和方法添加的图表.(堆栈和队列方法是为其赋予第一个值的构造函数)
I think I explained it terribly but please help T-T this is the chart of my classes and methods i need to add for each. (Stack and queue method are constructor to give first value to it)
推荐答案
您的类可以具有数组成员.例如:
Your class can have an array member. For example:
class CarStack {
Car[] cars = new Car[7];
int top = 0;
void Push(Car c) {
if (top != cars.Length) {
cars[top++] = c;
}
}
Car Pop() {
if (top != 0) return cars[--top];
else return null;
}
}
程序中的其他地方:
void CreateCarAndAddToStack(CarStack stack, String model, Color color)
{
Car c = new Car(model, color);
stack.Push(c);
}
这篇关于如何在C#中基于构造函数方法创建数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!