使用(键,值)实现栈

huangapple go评论138阅读模式
英文:

Implementing Stack With (Key,Value)

问题

I am wondering if I can implement a stack with (Key, Value) like below:

我想知道是否可以像下面这样使用(键,值)来实现一个堆栈:

  1. public static void main(String[] args) {
  2. PriorityQueueStack<Integer, V> s = new PriorityQueueStack<>();
  3. s.push(1, 'A');
  4. s.push(2, 'B');
  5. s.push(3, 'C');
  6. s.push(4, 'D');
  7. }

My class implementation is like below:

我的类实现如下:

  1. public class PriorityQueueStack<E> extends SortedPriorityQueue<Integer, E> implements
  2. PriorityQueue<Integer, E>{

Is there a way to implement it with (Key, Value) as I have searched I couldn't find any resource of such an implementation.

是否有办法以(键,值)的方式实现它?因为我搜索了一下,找不到这样的实现资源。

The output should be like this:

输出应该如下所示:

  1. (1,'A'), (2,'B'), ..... and so on
英文:

I am wondering if I can implement a stack with (Key, Value) like below:

  1. public static void main(String[] args) {
  2. PriorityQueueStack&lt;Integer,V&gt; s = new PriorityQueueStack&lt;&gt;();
  3. s.push(1,&#39;A&#39;);
  4. s.push(2,&#39;B&#39;);
  5. s.push(3,&#39;C&#39;);
  6. s.push(4,&#39;D&#39;);

My class implementation is like below:

  1. public class PriorityQueueStack&lt;E&gt; extends SortedPriorityQueue&lt;Integer, E&gt; implements
  2. PriorityQueue&lt;Integer, E&gt;{

Is there a way to implement it with (Key, Value) as I have searched I couldn't find any resource of such an implementation.

The output should be like this:

  1. (1,&#39;A&#39;),(2,&#39;B&#39;)..... and so on

答案1

得分: 4

It could be done by creating a separate class for key-value pair and adding an object of that class in the Stack.

  1. 创建一个**用于键值对的独立类**,然后**将该类的对象添加到堆栈中**。
  1. class Pair{
  2. int key;
  3. char value;
  4. public Pair(int key,char value){
  5. this.key = key;
  6. this.value = value;
  7. }
  8. }
  9. public class Main {
  10. public static void main(String[] args) {
  11. Stack<Pair> stack = new Stack<>();
  12. stack.push(new Pair(1,'A'));
  13. stack.push(new Pair(2,'B'));
  14. stack.push(new Pair(3,'C'));
  15. stack.push(new Pair(4,'D'));
  16. // Pair p = stack.pop();
  17. // System.out.println(p.key+" "+p.value);
  18. }
  19. }
英文:

It could be done by creating a separate class for key-value pair and adding an object of that class in the Stack.

  1. class Pair{
  2. int key;
  3. char value;
  4. public Pair(int key,char value){
  5. this.key = key;
  6. this.value = value;
  7. }
  8. }
  9. public class Main {
  10. public static void main(String[] args) {
  11. Stack&lt;Pair&gt; stack = new Stack&lt;&gt;();
  12. stack.push(new Pair(1,&#39;A&#39;));
  13. stack.push(new Pair(2,&#39;B&#39;));
  14. stack.push(new Pair(3,&#39;C&#39;));
  15. stack.push(new Pair(4,&#39;D&#39;));
  16. // Pair p = stack.pop();
  17. // System.out.println(p.key+&quot; &quot;+p.value);
  18. }
  19. }

huangapple
  • 本文由 发表于 2020年8月2日 22:11:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定