首页 > 代码库 > java 写法推荐

java 写法推荐

1. for循环

         // 1         for (int i = 0; i < list.size(); i++) {            int item = list.get(i);            System.out.println("这是第" + (i+1) + "个:值为:" + item);        }        // 2        int j = 0;        for (int i : list) {            ++j;            System.out.println("这是第" + j + "个:值为:" + i);        }

推荐写法2

2. HashMap遍历

    Map<String,String> map=new HashMap<String,String>();        map.put("1", "value1");        map.put("2", "value2");        map.put("3", "value3");        map.put("4", "value4");
//第三种:推荐,尤其是容量大时 System.out.println("\n通过Map.entrySet遍历key和value"); for(Map.Entry<String, String> entry: map.entrySet()) { System.out.println("Key: "+ entry.getKey()+ " Value: "+entry.getValue()); }

3. 使用log4j的时候如何输出printStackTrace()的堆栈信息

log.error(e.getMessage(),e)

4. String拼接

StringBuilder tmpStr= new StringBuilder();tmpStr.append(tmpStr1).append(tmpStr2).append(tmpStr3);String aimStr = tmpStr.toString();

 

java 写法推荐