zoukankan      html  css  js  c++  java
  • Java 7 的 7 个新的 “酷” 特性

    1. switch 中使用字符串变量

    public void testStringSwitch(String direction) {
      switch (direction) {
         case "up":
         y--;
         break;
         case "down":
         y++;
         break;
         case "left":
         x--;
         break;
         case "right":
         x++;
         break;
        default:
        System.out.println("Invalid direction!");
        break;
      }
    }

    2. 简化泛型对象创建

    // Java 7 以前版本
    Map<String,Map<String,int>>m=new HashMap<String, Map<String,int>>();
    // Java 7
    Map<String, Map<String, int>> m = new HashMap<>();

    3. 多异常处理

    try {
        Class a = Class.forName("wrongClassName");
        Object instance = a.newInstance();
    } catch (ClassNotFoundException | IllegalAccessException |
       InstantiationException ex) {
       System.out.println("Failed to create instance");
    }

    4. 资源的自动释放

    try (BufferedReader in=new BufferedReader(new FileReader("test.txt")))
    {
        String line = null;
        while ((line = in.readLine()) != null) {
        System.out.println(line);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    5. 文件 IO API 的改进

    下面是列出一个文件中所有行并打印的方法
    List<String> lines =  Files.readAllLines(
    FileSystems.getDefault().getPath("test.txt"), StandardCharsets.UTF_8);
     
    for (String line : lines) System.out.println(line);




  • 相关阅读:
    alg--动态规划(dynamic planning)
    alg--分治法
    汇编-理解call,ret
    汇编--实验7
    leetCode笔记--binary tree
    leetCode笔记--(1)
    C#获取当前路径的方法如下
    VS2013 快捷键 与 RESHARPER 冲突
    使用Visual Studio 2013进行单元测试--初级篇
    VS 插件
  • 原文地址:https://www.cnblogs.com/kuyuyingzi/p/4266225.html
Copyright © 2011-2022 走看看