zoukankan      html  css  js  c++  java
  • java中的陷阱,看看你掉进去了么!

    以下内容来自 CSDN

    java中的陷阱,看看你掉进去了么! - Java / Java SE

    看了一段北风网的视频,总结几个经典的java陷阱给大家。

    答案在我的博客中:http://blog.csdn.net/ol_beta/archive/2010/05/17/5598867.aspx
    欢迎大家讨论!

    1、找奇数:

    public static boolean isOdd(int i){ 
        return i % 2 == 1; 
    }

    上面的方法真的能找到所有的奇数么?


    2、浮点数相减

    System.out.println(2.0-1.9);  
    System.out.println(2.0-1.9);

    上面会打印0.1么?


    3、交换 

    int x = 2010; 
    int y = 2012; 
    x^=y^=x^=y;
    System.out.println("x= " + x + "; y= " + y);

    x、y的值互换了么?


    4、字符和字符串

    System.out.println("H" + "a");   
    System.out.println('H' + 'a');

    上面两个语句输出结果相同么?


    5、无限循环

    public static final int END = Integer.MAX_VALUE; 
    public static final int START = END - 100; 
    public static void main(String[] args) {
        int count = 0; 
        for (int i = START; i <= END; i++) 
            count++; 
        System.out.println(count); 
        }

    上面程序运行的结果是什么?


    6、计数器问题

    int minutes = 0; 
    for (int ms = 0; ms < 60*60*1000; ms++) 
        if (ms % 60*1000 == 0) 
            minutes++; 
    System.out.println(minutes);

    结果跟你想的一样么?


    7、到底返回什么?

    public static boolean decision() { 
         try { 
            return true; 
        } finally { 
          return false; 
        } 
    } 

    true?false?


    8、错误里聚集遍历

    public static void main(String[] args) {
            Vector v = new Vector();
            v.add("one");
            v.add("two");
            v.add("three"); 
            v.add("four");
            Enumeration enume = v.elements();
            while (enume.hasMoreElements()){
                String s = (String) enume.nextElement();
                if (s.equals("two"))
                    v.remove("two");
                else{
                    System.out.println(s);
                }
            }
            System.out.println("What's really there...");
            enume = v.elements();
            while (enume.hasMoreElements()){
                String s = (String) enume.nextElement();
                System.out.println(s);            
            }
        }

    运行代码看看结果跟你想的一样么?

  • 相关阅读:
    SA练习题总结-篇一
    树上距离(树形DP)
    Codeforces Round #633(Div.2) E. Perfect Triples
    Codeforces Round #633 (Div. 2) D.Edge Weight Assignment
    问题 B: FZB(树形DP+边记忆化)
    【Matlab】自学笔记——基础知识篇
    【Python科学计算】Numpy——ndarry
    退役总结
    [树的遍历]树的遍历(PTA)
    [stl]集合相似度(PTA)
  • 原文地址:https://www.cnblogs.com/lanfengniao/p/3061094.html
Copyright © 2011-2022 走看看