1、NO.3 长整除public class LongDivision final long MICROS_PER_DAY = 24 l* 60 * 60 * 1000 * 1000; /微秒 final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; /毫秒 System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);NO.4 互换内容public class Swap int x = 1984; int y = 2001; x= y= x=y; System.out.println(x= + x + ; y= + y
2、); 陷阱等级:NO.5 字符串和字符public class CharAndString H + a); System.out.println(H + apublic class CharAndString public static void main(String args) System.out.println( System.out.println( NO.6 字符数组public class CharArray String letters = ABC char numbers = 1, 23; System.out.print(numbers); System.out.print
3、(letters + easy as +numbers);public class CharArray String letters = char numbers = System.out.print(letters + NO.7 转义字符public class UnicodeTest au0022.length() +u0022b.length();public class UnicodeTest 陷阱等级:NO.8 打印输出类名public class MyClass System.out.println(MyClass.class.getName().replaceAll(.,/) +
4、 .classpublic class MyClass NO.9 随机数的问题import java.util.Random;public class RandomTest private static Random rnd = new Random(); StringBuffer word = null; switch(rnd.nextInt(2) case 1: word = new StringBuffer(P case 2:G default:M word.append(in System.out.println(word);public class RandomTest NO.10
5、无情的增量操作public class ForTest int j = 0; for (int i = 0; i 100; i+) j = j+; System.out.println(j);public class ForTest NO.11 整数边界的问题public class WhileTest public static final int END = Integer.MAX_VALUE; public static final int START = END - 100; int count = 0; for (int i = START;= END; i+) count+; Sy
6、stem.out.println(count);public class WhileTest int count = 0; for (int i = START; count+; System.out.println(count);NO.12 计数器的问题public class Clock int minutes = 0; for (int ms = 0; ms 0) out.write(buf, 0, n); if (in != null) in.close(); if (out != null) out.close();public class IOTest 5、无限循环 view pl
7、aincopy to clipboardprint?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;count+;System.out.println(count); 上面程序运行的结果是什么?A:无限循环。将i=END改成iEND?为什么呢?你知道的,呵呵!6、计数器问题 int minutes = 0;for (int m
8、s = 0;if (ms % 60*1000 = 0) minutes+;System.out.println(minutes);结果跟你想的一样么?A:呵呵,括号问题,不多说!7、到底返回什么?public static boolean decision() try return true; finally return false;true?false?一般情况下,不管怎么说try/catch代码块中,finally总是最后被执行的 。8、错误里聚集遍历 Vector v = new Vector();v.add(onetwothreefourEnumeration enume = v.e
9、lements();while (enume.hasMoreElements() String s = (String) enume.nextElement();if (s.equals() v.remove(else System.out.println(s);System.out.println(Whats really there.enume = v.elements();运行代码看看结果跟你想的一样么?一般不建议在遍历聚集的时候对聚集进行操作。为什么结果是这样呢?看JDK源码能得到答案。Enumeration没有实现Fail Fast操作,如果换成ArrayList,上面的代码可能会出错。java与模式迭代子(iterator)介绍了。public String substring(int beginIndex, int endIndex) if (beginIndex count) throw new StringIndexOutOfBoundsException(endIndex); if (beginIndex endIndex) throw new StringIndexOutOfBoundsException(endI
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2