zoukankan      html  css  js  c++  java
  • Algs4-1.3.12在可迭代Stack用例中写一个copy栈方法

     1.3.12编写一个可迭代的Stack用例,它含有一个静态的copy()方法,接受一个字符串的栈作为参数,并返回该栈的一个副本。注意:这种能是迭代器价值的一个重要体现,因为有了它我们无需改变基本API就能够实现这种功能。
    答:
    图片
    public class test
    {
       public static void main(String[] args)
       {
           Stack<String> s=new Stack<String>();
           while(!StdIn.isEmpty())
           {
               String item=StdIn.readString();
               s.push(item);
            }
            //
            StdOut.println("Stack of s before copy:");
               show(s);
            //
               Stack<String> s2=copy(s);
               StdOut.println("Stack of s after copy:");
               show(s);
               StdOut.println("Stack of s copy:");
               show(s2);
         
        }//end main
       public static Stack<String> copy(Stack<String> s)
       {
           Stack<String> tempStack=new Stack<String>();
           Stack<String> copyStack=new Stack<String>();
           for(String item:s)
               tempStack.push(item);
          
           for(String item:tempStack)
               copyStack.push(item);
           return copyStack;
          
       }
       private static void show(Stack<String> s)
       {
           for(String item:s)
               StdOut.print(item);
           StdOut.println();
       }
    }//end class

  • 相关阅读:
    Activity相关知识点总结
    大端和小端
    两年前端感悟
    线性结构与树形结构相互转换(ES6实现)
    基于webpack的React项目搭建(三)
    MySQL安装之yum安装
    EL表达式中fn函数
    配置Log4j 详解
    Canvas学习:封装Canvas绘制基本图形API
    canvas
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849408.html
Copyright © 2011-2022 走看看