Java – how to write a method that accepts stacks and queues?
•
Java
So I wrote this very big method If you give it a stack, it will return a stack If you give it a queue, it will return a queue It uses a lot of recursion and accepts the queue / stack and returns the corresponding same queue / stack
I don't want to copy / paste my method, so I can change the type used internally, so what method can make this general? In, will it accept any old collection and play with it? I tried to use collection, but the problem was that it didn't Remove() I can use stack / queue
Any help would be appreciated
thank you.
Solution
You can use collection, but there are special cases around your deletion Of course, when you get a series that is not one of the two, you have to figure out what to do
if (myCollection instanceof Queue) { ((Queue)myCollection).remove(); } else if (myCollection instanceof Stack) { ((Stack)myCollection).remove(thingy); } else { // Oops! Now what? }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码