Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

How can i create an array based on constructor method in c#?

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...

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...

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }
}

And elsewhere in the program:

void CreateCarAndAddToStack(CarStack stack, String model, Color color)
{
    Car c = new Car(model, color);
    stack.Push(c);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...