list和map遍历代码-程序员宅基地

技术标签: java  

list和map遍历代码

代码如下

import java.util.*;


public class Main {

    public static void main(String[] args) {
        //list遍历的三种方式
        //1.for循环
        List<String> list =new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        for (int i=0;i<list.size();i++){
            if(list.get(i).equals("11")){
                list.remove(list.get(i));
            }
        }
        System.out.println("list是指"+list);

        List<String> list1=new ArrayList<String>();
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("4");
        list1.add("5");
        list1.add("6");
        //2.增强for循环,foreach
        for(String str:list1){
            if(str.equals("22")) {
                list.remove(str);
            }
        }
        System.out.println("list1是指"+list1);
        List list2=new ArrayList();
        list2.add("1");
        list2.add("2");
        list2.add("3");
        list2.add("4");
        list2.add("5");
        list2.add("6");
        //3.迭代器
        Iterator<String> iterator=list2.iterator();
        while(iterator.hasNext()){
            String x=iterator.next();
            if(x.equals("333")){
                iterator.remove();
            }
        }
        System.out.println("list2是指"+list2);

        //map的遍历方式
        //1.通过map.keySet遍历key和value
        Map<String,String> map=new HashMap<String,String>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");
        map.put("4","d");
        map.put("5","e");
        for(String key:map.keySet()){
            System.out.println("map的key"+key+"map的value"+map.get(key));
        }
        //迭代器遍历map.
        Iterator iterator1=map.entrySet().iterator();
        while (iterator1.hasNext()){
            Map.Entry<String,String> entry= (Map.Entry<String, String>) iterator1.next();
            System.out.println("map的key是"+entry.getKey()+"map的value是"+entry.getValue());
        }
        //直接使用entrySet
        for (Map.Entry<String,String> entry:map.entrySet()){
            System.out.println("map的key是"+entry.getKey()+"map的value是"+entry.getValue());
        }
        //遍历所有的values
        int i = 0;
        for (String value:map.values()) {

            i+= 1;
            String str=null;
            str=String.format("The %d value is %s",i, value);
            System.out.println(str);
        }

    }
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43537420/article/details/104370341

智能推荐

zabbix学习笔记-程序员宅基地

zabbix1. 相关介绍1.1 百度百科1.1.1 简介 “zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。 zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。 zabbix由2部分构成,zabbix ...

iOS使用XZMRefresh实现UITableView或UICollectionView横向刷新-程序员宅基地

https://blog.csdn.net/u013285730/article/details/50615551?utm_source=blogxgwz6XZMRefreshThe easiest way to use pull-to-The transverse refresh(非常易用的横向刷新框架与MJRefresh用法一致)框架开发的缘由:现今...

oracle alter user identified,Oracle 技术之ALTER USER的REPLACE语句_南方姑娘走在成都街头的博客-程序员宅基地

ALTER USER语句有一个可选的REPLACE语句,用来在启动密码验证函数后,输入原始密码。默认情况下,用户修改自己的密码不需要提供当前密码:SQL> create user test identified by test;用户已创建。SQL> grant connect to test;授权成功。SQL> select * from dba_profiles;PROFILE...

jasypt在springboot项目中遇到异常:Error creating bean with name ‘enableEncryptablePropertySourcesPostProc_springboot 加入com.github.ulisesbocchio 错误_knight11112的博客-程序员宅基地

jasypt在springboot项目中遇到异常:Error creating bean with name 'enableEncryptablePropertySourcesPostProc背景在使用jasypt对spring boot的配置文件中的敏感信息进行加密处理时,使用stater直接启动时,遇到了一个异常<dependency> <groupId>com.github.ulisesbocchio</groupId> <art_springboot 加入com.github.ulisesbocchio 错误

系统实现与测试-程序员宅基地

总述:这部分简单描述了软件测试部分需要完成的活动,这些活动需要遵循一些规则。系统实现是项目管理重点关注的地方,系统测试一方面是为了检查实际情况和要求之间的差距提供纠错的基础信息,另一方面是为了最后的系统交付做准备。1、系统实现答:系统实现阶段是将设计的系统实施的过程。程序设计方法是软件工程方法学的主要内容之一,主要有结构化程序设计、面向对象程序设计、面向方面的程序设计、可视化程序设计。程序设计语言的具体选择需要基于系统设计的基础上进行考虑。2、软件测试答:软件测试是发现软件错误(.._系统实现

算法 | 深度分析二分查找算法_二分查找和深搜查找区别-程序员宅基地

编者荐语:大家好,我是你们的朋友 朋哥。二分查找我一直 都认为 算法 很简单 当然包括二分查找也很简单,但事实真的如此吗?二分查找真的很简单吗?其实并不简单。看看 Knuth 大佬(发明 KMP 算法的那位)怎么说的:Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky...这句话可以这样理解:思路很简单,细节._二分查找和深搜查找区别

随便推点

omnicore api速查表-程序员宅基地

Omni Core是比特币核心的一个分支,它在比特币协议之上实现了一个 新的Omni协议层。因此Omni Core的API交互的方式和比特币核心的 方式一样,只是添加了一些额外的Omni协议特性相关的RPC调用。本文提供omni api的速查表。如果要快速掌握比特币的对接与应用开发,推荐汇智网的在线互动课程:Java比特币开发详解 ----- Php比特币开发详解----- C#比特币开发..._omnicore api

图像特征点—SIFT特征点-程序员宅基地

图像特征点—SIFT特征点关于SIFT特征点,以下三篇博客说的非常详细,再次不再赘述。https://mp.weixin.qq.com/s/XVB88f119gRqpwTTRvTyrAhttps://mp.weixin.qq.com/s/Vx-8xsXd6aXoJupr0BEMowhttps://mp.weixin.qq.com/s/8WAgDmc1R_9rGRjM9eanDgOpenC...

Apply Bug10010310 On Oracle RAC 10.2.0.5-程序员宅基地

9月24日数据库上频繁出现例如以下错误Errors in file /u04/admin/njord/udump/njord_ora_25895.trc:ORA-27300: OS system dependent operation:invalid_process_id failed with status: 0ORA-27301: OS failure messag...

神经网络——Conv2d的使用-程序员宅基地

在Convolution Layers 卷积层中有很多函数,像:nn.Conv1d 表示1维的;nn.Conv2d 表示2维的,如图片,等。其中Conv2d使用最多,故本文重点讲下nn.Conv2d的使用。_conv2d

ROS rosdep更新失败问题_error: your rosdep installation has not been initi_长沙有肥鱼的博客-程序员宅基地

reading in sources list data from /etc/ros/rosdep/sources.list.dERROR: unable to process source [https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml]: <urlopen error [Errno 104] Connection reset by peer> (https://raw.gi._error: your rosdep installation has not been initialized yet. please run: ro

IP_t_ip_t include-程序员宅基地

/include/net.h/* * Internet Protocol (IP) header. */typedef struct { uchar ip_hl_v; //协议版本,协议头长度 uchar ip_tos; //数据报优先级 ushort_ip_t include