zoukankan      html  css  js  c++  java
  • java中for循环问题

    1.使用for循环遍历Map类型

     1 import java.util.HashMap;
     2 import java.util.Map;
     3 
     4 import org.junit.Test;
     5 
     6 
     7 public class TestJava {
     8 
     9     @Test
    10     public void testMap() {
    11         Map<String,String> ma = new HashMap();
    12         ma.put("1111", "apple");
    13         ma.put("2222", "chair");
    14         ma.put("3333", "banana");
    15         for(Map.Entry<String, String> m:ma.entrySet()){
    16             System.out.println(m.getKey()+"->"+m.getValue());
    17         }
    18     }
    19 }

    2.用for遍历数组的时候,用冒号for循环和传统for循环的时候,用的临时变量有一个区别

    如下,用:循环的时候,如果临时变量在for外部定义,会报错

          int[] arrs=new int[]{1,3,4,5,6};
          int i=0;
          for( i:arrs){
              System.out.println(i);
          }

    而用传统的for循环却可以在外部声明

          int[] arrs=new int[]{1,3,4,5,6};
          int i=0;
          for(;i<arrs.length;i++){
              System.out.println(arrs[i]);
          }

    还不确定是什么原因,或许是因为前者是对某一位置数组值的引用,而后者是对指定位置的索引的引用???

    留待以后解决。

    我的博客:www.while0.com

    我的博客:www.shishangguan.net

  • 相关阅读:
    基于Flask开发web微信
    爬取实例
    scrapy框架学习之路
    scripy
    wtforms
    由testcase数据之分析
    无用之flask学习
    无用之flask
    无用之学matplotlib,numpy,pandas
    jsp_1
  • 原文地址:https://www.cnblogs.com/yamadie/p/2882263.html
Copyright © 2011-2022 走看看