首页 > 其他分享 >stack 的foreach迭代

stack 的foreach迭代

时间:2022-11-06 08:44:05浏览次数:35  
标签:迭代 int System foreach println array stack out

stack底层为数组, foreach和iterator迭代时遍历数组输出,

public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    /**
     * The array buffer into which the components of the vector are
     * stored. The capacity of the vector is the length of this array buffer,
     * and is at least large enough to contain all the vector's elements.
     *
     * <p>Any array elements following the last element in the Vector are null.
     *
     * @serial
     */
    protected Object[] elementData;

  

所以遍历的顺序是从栈低到栈顶, 和 pop的顺序正好相反

@Test
    public void t () {
        Stack<Integer> s = new Stack<Integer>();
        System.out.println("输入");
        s.push(1);
        System.out.println("1");
        s.push(2);
        System.out.println("2");
        s.push(3);
        System.out.println("3");
        
        System.out.println("foreach");
        for(int i : s){
          System.out.println(i);
        }
        
        System.out.println("Iterator");
        Iterator<Integer> t = s.iterator();
        while(t.hasNext()){
          int i = t.next();
          System.out.println(i);
        }
        
        System.out.println("弹出");
        while(!s.isEmpty()){
          int j = s.pop();
          System.out.println(j);
        }
        
    }

  

输入
1
2
3
foreach
1
2
3
Iterator
1
2
3
弹出
3
2
1

标签:迭代,int,System,foreach,println,array,stack,out
From: https://www.cnblogs.com/yanher/p/16861884.html

相关文章

  • 【重识云原生】第三章云存储3.4节——OpenStack Swift 对象存储方案
      《重识云原生系列》专题索引:第一章——不谋全局不足以谋一域第二章计算第1节——计算虚拟化技术总述第二章计算第2节——主流虚拟化技术之VMareESXi第二章计算第3......
  • 18.迭代器模式
    [实验任务一]:JAVA和C++常见数据结构迭代器的使用信1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,......
  • 递归转迭代
    参考参考文章例题对称二叉树反转链表翻转二叉树验证二叉搜索树......
  • JAVA8-Lambda-forEach遍历List/Map
    一、遍历List代码示例publicstaticvoidmain(String[]args){List<String>list=Arrays.asList("北","上","广","深");list.forEach(System.out::prin......
  • openstack单机部署
    注:centos8单机版 注:本次实验手动配置密码均为admin环境准备:配置hosts文件192.168.116.8为本机IPecho'192.168.116.8controllervipmyip'>>/etc/hostsyumupgrad......
  • OpenStack Neutron浅析
    1.基础知识1.1防火墙(firewall)防火墙是依照特定的规则来控制进出它的网络流量的网络安全系统。一个典型的场景是在一个受信任的内网和不受信任的外网比如Internet之间......
  • 软件设计-迭代器模式
    JAVA和C++常见数据结构迭代器的使用信1305班共44名同学,每名同学都有姓名,学号和年龄等属性,分别使用JAVA内置迭代器和C++中标准模板库(STL)实现对同学信息的遍历,要求按照学号从......
  • 迭代器失效的几种情况
    关于迭代器失效,,今天做一个总结。迭代器失效分三种情况考虑,也是三种数据结构考虑,分别为数组型,链表型,树型数据结构。1、对于序列式容器,比如vector,删除当前的iterator会使后面......
  • python-迭代器与生成器
    迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。迭代器有两个基本的方法:iter()和next()。字符串,列表或元组对象都可用于创建......
  • 2022-11-04 js foreach 三重循环
    letarr=[];req.data.forEach((e,k1)=>{letobj={};obj.value=e.content;obj.children=[];obj.expand=false;if(e.children){e.child......