java 写法推荐

1. for循环

         //
for (int i = 0; i < list.size(); i++) {
int item = list.get(i);
System.out.println("这是第" + (i+1) + "个:值为:" + item);
} //
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();

5. 多维数组

package com.example.ch6_2;

public class testArray {
static final int[][][] THREEARRAY = new int[][][] {
{{1, 2}, {11, 12, 13}},
{{2}, {9, 10}},
{{6, 7}, {14, 16}},
{{6}, {9, 10}},
}; static final int[][] TWOARRAY = new int[][] {
{1, 2},
{11, 12, 13}
}; static final int[] ONEARRAY = new int[] {1, 2, 11, 12, 15}; public static void main(String[] argv) {
System.out.println("\nONEARRAY:");
for (int oneKey : ONEARRAY) {
System.out.println(oneKey);
} System.out.println("\nTWOARRAY:");
for (int[] oneKey : TWOARRAY) {
for (int key : oneKey) {
System.out.print(key);
}
System.out.println();
} System.out.println("\nTHREEARRAY:");
for (int[][] oneKey : THREEARRAY) {
for (int[] key : oneKey) {
for (int tmp : key) {
System.out.print(tmp);
}
System.out.println();
}
System.out.println("\n");
}
}
}

结果

ONEARRAY:
1
2
11
12
15 TWOARRAY:
12
111213 THREEARRAY:
12
111213 2
910 67
1416 6
910 Process finished with exit code 0

  

上一篇:egret3.x升级5.2


下一篇:一、基础篇--1.1Java基础-final, finally, finalize 的区别