几个陷阱题Word下载.docx

上传人:b****2 文档编号:5952278 上传时间:2023-05-05 格式:DOCX 页数:28 大小:20.64KB
下载 相关 举报
几个陷阱题Word下载.docx_第1页
第1页 / 共28页
几个陷阱题Word下载.docx_第2页
第2页 / 共28页
几个陷阱题Word下载.docx_第3页
第3页 / 共28页
几个陷阱题Word下载.docx_第4页
第4页 / 共28页
几个陷阱题Word下载.docx_第5页
第5页 / 共28页
几个陷阱题Word下载.docx_第6页
第6页 / 共28页
几个陷阱题Word下载.docx_第7页
第7页 / 共28页
几个陷阱题Word下载.docx_第8页
第8页 / 共28页
几个陷阱题Word下载.docx_第9页
第9页 / 共28页
几个陷阱题Word下载.docx_第10页
第10页 / 共28页
几个陷阱题Word下载.docx_第11页
第11页 / 共28页
几个陷阱题Word下载.docx_第12页
第12页 / 共28页
几个陷阱题Word下载.docx_第13页
第13页 / 共28页
几个陷阱题Word下载.docx_第14页
第14页 / 共28页
几个陷阱题Word下载.docx_第15页
第15页 / 共28页
几个陷阱题Word下载.docx_第16页
第16页 / 共28页
几个陷阱题Word下载.docx_第17页
第17页 / 共28页
几个陷阱题Word下载.docx_第18页
第18页 / 共28页
几个陷阱题Word下载.docx_第19页
第19页 / 共28页
几个陷阱题Word下载.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

几个陷阱题Word下载.docx

《几个陷阱题Word下载.docx》由会员分享,可在线阅读,更多相关《几个陷阱题Word下载.docx(28页珍藏版)》请在冰点文库上搜索。

几个陷阱题Word下载.docx

★★★★★

NO.3长整除

publicclassLongDivision{

finallongMICROS_PER_DAY=24l*60*60*1000*1000;

//微秒

finallongMILLIS_PER_DAY=24*60*60*1000;

//毫秒

System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);

NO.4互换内容

publicclassSwap{

intx=1984;

inty=2001;

x^=y^=x^=y;

System.out.println("

x="

+x+"

;

y="

+y);

陷阱等级:

★★☆☆☆

NO.5字符串和字符

publicclassCharAndString{

H"

+"

a"

);

System.out.println('

H'

+'

a'

publicclassCharAndString{

publicstaticvoidmain(String[]args){

System.out.println("

System.out.println('

}

}

NO.6字符数组

publicclassCharArray{

Stringletters="

ABC"

char[]numbers={'

1'

'

2'

3'

};

System.out.print(numbers);

System.out.print(letters+"

easyas"

+numbers);

publicclassCharArray{

Stringletters="

char[]numbers={'

System.out.print(letters+"

★★★★☆

NO.7转义字符

publicclassUnicodeTest{

a\u0022.length()+\u0022b"

.length());

publicclassUnicodeTest{

}

}陷阱等级:

NO.8打印输出类名

publicclassMyClass{

System.out.println(MyClass.class.getName().replaceAll("

."

"

/"

)+"

.class"

publicclassMyClass{

NO.9随机数的问题

importjava.util.Random;

publicclassRandomTest{

privatestaticRandomrnd=newRandom();

StringBufferword=null;

switch(rnd.nextInt

(2)){

case1:

word=newStringBuffer("

P"

case2:

G"

default:

M"

word.append('

i'

n'

System.out.println(word);

publicclassRandomTest{

NO.10无情的增量操作

publicclassForTest{

intj=0;

for(inti=0;

i<

100;

i++){

j=j++;

System.out.println(j);

publicclassForTest{

NO.11整数边界的问题

publicclassWhileTest{

publicstaticfinalintEND=Integer.MAX_VALUE;

publicstaticfinalintSTART=END-100;

intcount=0;

for(inti=START;

=END;

i++)

count++;

System.out.println(count);

publicclassWhileTest{

intcount=0;

for(inti=START;

count++;

System.out.println(count);

NO.12计数器的问题

publicclassClock{

intminutes=0;

for(intms=0;

ms<

60*60*1000;

ms++)

if(ms%60*1000==0)

minutes++;

System.out.println(minutes);

publicclassClock{

NO.13优柔寡断的返回值

publicclassReturnValue{

System.out.println(decision());

publicstaticbooleandecision(){

try{

returntrue;

}finally{

returnfalse;

publicclassReturnValue{

System.out.println(decision());

NO.14你好,再见

publicclassGoodBye{

Helloworld"

System.exit(0);

Goodbyeworld"

publicclassGoodBye{

NO.15到底关闭了吗

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

publicclassIOTest{

publicstaticvoidcopy(Stringsrc,Stringdest)throwsIOException{

InputStreamin=null;

OutputStreamout=null;

in=newFileInputStream(src);

out=newFileOutputStream(dest);

byte[]buf=newbyte[1024];

intn;

while((n=in.read(buf))>

0)

out.write(buf,0,n);

if(in!

=null)in.close();

if(out!

=null)out.close();

publicclassIOTest{

5、无限循环

  viewplaincopytoclipboardprint?

  publicstaticfinalintEND=Integer.MAX_VALUE;

  publicstaticfinalintSTART=END-100;

  publicstaticvoidmain(String[]args){

  intcount=0;

  for(inti=START;

  count++;

  System.out.println(count);

  }

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

  A:

无限循环。

将i<

=END改成i<

END?

为什么呢?

你知道的,呵呵!

6、计数器问题

  intminutes=0;

  for(intms=0;

  if(ms%60*1000==0)

  minutes++;

  System.out.println(minutes);

  结果跟你想的一样么?

  A:

呵呵,括号问题,不多说!

  7、到底返回什么?

  publicstaticbooleandecision(){

  try{

  returntrue;

  }finally{

  returnfalse;

  true?

false?

一般情况下,不管怎么说try/catch代码块中,finally总是最后被执行的。

  8、错误里聚集遍历

  Vectorv=newVector();

  v.add("

one"

two"

three"

four"

  Enumerationenume=v.elements();

  while(enume.hasMoreElements()){

  Strings=(String)enume.nextElement();

  if(s.equals("

))

  v.remove("

  else{

  System.out.println(s);

  System.out.println("

What'

sreallythere..."

  enume=v.elements();

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

一般不建议在遍历聚集的时候对聚集进行操作。

为什么结果是这样呢?

看JDK源码能得到答案。

Enumeration没有实现FailFast操作,如果换成ArrayList,上面的代码可能会出错。

《java与模式》迭代子(iterator)介绍了。

publicStringsubstring(intbeginIndex,intendIndex){

if(beginIndex<

0){

thrownewStringIndexOutOfBoundsException(beginIndex);

if(endIndex>

count){

thrownewStringIndexOutOfBoundsException(endIndex);

if(beginIndex>

endIndex){

thrownewStringIndexOutOfBoundsException(endI

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 表格模板 > 书信模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2