黑马入学测试面试题答案整理解析.docx

上传人:b****3 文档编号:11208795 上传时间:2023-05-29 格式:DOCX 页数:22 大小:21.63KB
下载 相关 举报
黑马入学测试面试题答案整理解析.docx_第1页
第1页 / 共22页
黑马入学测试面试题答案整理解析.docx_第2页
第2页 / 共22页
黑马入学测试面试题答案整理解析.docx_第3页
第3页 / 共22页
黑马入学测试面试题答案整理解析.docx_第4页
第4页 / 共22页
黑马入学测试面试题答案整理解析.docx_第5页
第5页 / 共22页
黑马入学测试面试题答案整理解析.docx_第6页
第6页 / 共22页
黑马入学测试面试题答案整理解析.docx_第7页
第7页 / 共22页
黑马入学测试面试题答案整理解析.docx_第8页
第8页 / 共22页
黑马入学测试面试题答案整理解析.docx_第9页
第9页 / 共22页
黑马入学测试面试题答案整理解析.docx_第10页
第10页 / 共22页
黑马入学测试面试题答案整理解析.docx_第11页
第11页 / 共22页
黑马入学测试面试题答案整理解析.docx_第12页
第12页 / 共22页
黑马入学测试面试题答案整理解析.docx_第13页
第13页 / 共22页
黑马入学测试面试题答案整理解析.docx_第14页
第14页 / 共22页
黑马入学测试面试题答案整理解析.docx_第15页
第15页 / 共22页
黑马入学测试面试题答案整理解析.docx_第16页
第16页 / 共22页
黑马入学测试面试题答案整理解析.docx_第17页
第17页 / 共22页
黑马入学测试面试题答案整理解析.docx_第18页
第18页 / 共22页
黑马入学测试面试题答案整理解析.docx_第19页
第19页 / 共22页
黑马入学测试面试题答案整理解析.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

黑马入学测试面试题答案整理解析.docx

《黑马入学测试面试题答案整理解析.docx》由会员分享,可在线阅读,更多相关《黑马入学测试面试题答案整理解析.docx(22页珍藏版)》请在冰点文库上搜索。

黑马入学测试面试题答案整理解析.docx

黑马入学测试面试题答案整理解析

1.定义一个交通灯枚举,包含红灯、绿灯、黄灯,需要有获得下一个灯的方法;

例如:

红灯获取下一个灯是绿灯,绿灯获取下一个灯是黄灯。

publicenumLamp{

RED("GREEN"),GREEN("YELLOW"),YELLOW("RED");

privateStringnext;

privateLamp(Stringnext){

this.next=next;

}

publicLampnextLamp(){

returnLamp.valueOf(next);

}

}

2、写一个ArrayList类的代理,实现和ArrayList中完全相同的功能,并可以计算每个方法运行的时间。

publicclasstest1{

publicstaticvoidmain(String[]args){

finalArrayListtarget=newArrayList();

Listproxy=(List)Proxy.newProxyInstance(

List.class.getClassLoader(),

ArrayList.class.getInterfaces(),

newInvocationHandler(){

@Override

publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)

throwsThrowable{

longbeginTime=System.currentTimeMillis();

Thread.sleep(10);

ObjectreVal=method.invoke(target,args);

longendTime=System.currentTimeMillis();

System.out.println(method.getName()+"runingtimeis"+(endTime-beginTime));

returnreVal;

}

});

proxy.add("nihaoa");

proxy.add("nihaoa");

proxy.add("nihaoa");

proxy.remove("nihaoa");

System.out.println(proxy.toString());

}

}

3.ArrayListlist=newArrayList();

在这个泛型为Integer的ArrayList中存放一个String类型的对象。

publicclasstest2{

publicstaticvoidmain(String[]args)throwsException{

ArrayListlist=newArrayList();

Methodmethod=list.getClass().getMethod("add",Object.class);

method.invoke(list,"iamaString");

System.out.println(list.toString());

}

}

4、一个ArrayList对象aList中存有若干个字符串元素,

现欲遍历该ArrayList对象,

删除其中所有值为"abc"的字符串元素,请用代码实现。

publicclasstest4{

publicstaticvoidmain(String[]args){

ArrayListaList=newArrayList();

aList.add("abc");

aList.add("nihaoa");

aList.add("nihaoa");

aList.add("abc");

aList.add("cdb");

aList.add("abc");

aList.add("cdb");

System.out.println(aList.toString());

Iteratorit=aList.iterator();

while(it.hasNext()){

Stringstr=it.next();

if(str.equals("abc")){

it.remove();

}

}

System.out.println(aList.toString());

}

}

5、编写一个类,增加一个实例方法用于打印一条字符串。

并使用反射手段创建该类的对象,并调用该对象中的方法。

publicclasstest5{

publicstaticvoidmain(String[]args)throwsException{

Classclazz=myClass.class;

Methodmethod=clazz.getMethod("printStr",String.class);

method.invoke(clazz.newInstance(),"nihaoma?

thismyprintstr");

}

}

classmyClass{

publicvoidprintStr(Stringstr){

System.out.println(str);

}

}

6、存在一个JavaBean,它包含以下几种可能的属性:

1:

boolean/Boolean

2:

int/Integer

3:

String

4:

double/Double属性名未知,现在要给这些属性设置默认值,以下是要求的默认值:

String类型的默认值为字符串int/Integer类型的默认值为100boolean/Boolean类型的默认值为true

double/Double的默认值为0.01D.

只需要设置带有getXxx/isXxx/setXxx方法的属性,非JavaBean属性不设置,请用代码实现

publicclasstest7{

publicstaticvoidmain(String[]args)throwsException{

Classclazz=Class.forName("cn.heima.test.testBean");

Objectbean=clazz.newInstance();

BeanInfobeanInfo=Introspector.getBeanInfo(clazz);

//System.out.println(beanInfo);

PropertyDescriptor[]propertyDescriptors=beanInfo

.getPropertyDescriptors();

for(PropertyDescriptorpd:

propertyDescriptors){

//System.out.println(pd);

//获取属性名

Objectname=pd.getName();

//获取属性类型

Objecttype=pd.getPropertyType();

//获取get方法

MethodgetMethod=pd.getReadMethod();

//获取set方法

MethodsetMethod=pd.getWriteMethod();

if(!

"class".equals(name)){

if(setMethod!

=null){

if(type==boolean.class||type==Boolean.class){

setMethod.invoke(bean,true);

}

if(type==String.class){

setMethod.invoke(bean,"");

}

if(type==int.class||type==Integer.class){

setMethod.invoke(bean,100);

}

if(type==double.class||type==Double.class){

setMethod.invoke(bean,0.01D);

}

}

if(getMethod!

=null){

System.out.println(type+""+name+"="

+getMethod.invoke(bean,null));

}

}

}

}

}

classtestBean{

privatebooleanb;

privateIntegeri;

privateStringstr;

privateDoubled;

publicBooleangetB(){

returnb;

}

publicvoidsetB(Booleanb){

this.b=b;

}

publicIntegergetI(){

returni;

}

publicvoidsetI(Integeri){

this.i=i;

}

publicStringgetStr(){

returnstr;

}

publicvoidsetStr(Stringstr){

this.str=str;

}

publicDoublegetD(){

returnd;

}

publicvoidsetD(Doubled){

this.d=d;

}

}

7、定义一个文件输入流,调用read(byte[]b)

方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5,不考虑中文编码问题)。

publicclasstest8{

publicstaticvoidmain(String[]args){

FileInputStreamfr=null;

try{

fr=newFileInputStream("d:

/exercise.txt");

byte[]bt=newbyte[5];

intlen=0;

while((len=fr.read(bt))!

=-1){

for(inti=0;i

System.out.print((char)bt[i]);

}

}

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}finally{

if(fr!

=null){

try{

fr.close();

}catch(IOExceptione){

e.printStackTrace();

}finally{

fr=null;

}

}

}

}

}

8、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。

这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。

提示:

十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2

,这次得到的余数就是次低位,如此循环,直到被除数为0为止。

其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),就很容易理解十进制数转二进制数的这种方式。

publicclasstest9{

publicstaticvoidmain(String[]args){

Scannersc=null;

while(true){

sc=newScanner(System.in);

Stringstr=sc.nextLine();

inta=0;

if(isOctNumers(str)){

a=Integer.valueOf(str);

}else{

System.out.println("输入不正确,请重新输入");

continue;

}

System.out.println(toBinary(a));

sc.close();

}

}

privatestaticbooleanisOctNumers(Stringstr){

try{

Integer.parseInt(str);

returntrue;

}catch(NumberFormatExceptione){

returnfalse;

}

}

publicstaticStringtoBinary(Integerdecimal){

StringBuildersb=newStringBuilder();

intx=0;

while(decimal!

=0){

x=decimal%2;

decimal=(int)(decimal/2);

sb.append(x);

}

sb.reverse();

returnsb.toString();

}

}

9、金额转换,阿拉伯数字转换成中国传统形式。

例如:

101000001010转换为壹仟零壹拾亿零壹仟零壹拾圆整

publicclasstestt10{

 

privatestaticfinalchar[]data={'零','壹','贰','叄','肆','伍','陆',

'柒','捌','玖'};

privatestaticfinalchar[]units={'圆','拾','佰','仟','万','拾','佰',

'仟','亿','拾','佰','仟'};

@SuppressWarnings("resource")

publicstaticvoidmain(String[]args){

while(true){

Scannersc=newScanner(System.in);

longl=sc.nextLong();

System.out.println(convert(l));

}

}

publicstaticStringconvert(longmoney){

StringBuffersbf=newStringBuffer();

intuint=0;

while(money!

=0){

sbf.insert(0,units[uint++]);

sbf.insert(0,data[(int)(money%10)]);

money=money/10;

}

//去零

returnsbf.toString().replaceAll("零[仟佰拾]","零").replaceAll("零+万","万")

.replaceAll("零+亿","亿").replaceAll("亿万","亿零")

.replaceAll("零+","零").replaceAll("零圆","圆");

}

}

10.取出一个字符串中字母出现的次数。

如:

字符串:

"abcde%^kka27qoq",输出格式为:

a

(2)b

(1)k

(2)...

publicclasstest1{

publicstaticvoidmain(String[]args){

Stringstr="abcdekka27qoA*&AAAq";

CountChar(str);

}

privatestaticvoidCountChar(Stringstr){

char[]c=str.toCharArray();

System.out.println(c);

Mapmap=newLinkedHashMap();

for(inti=0;i

if((c[i]<=90&&c[i]>=65)||(c[i]>=97&&c[i]<=112)){

if(!

(map.keySet().contains(c[i]))){

map.put(c[i],1);

}else{

map.put(c[i],map.get(c[i])+1);

}

}

}

StringBuildersb=newStringBuilder();

Iterator>it=map.entrySet().iterator();

while(it.hasNext()){

Map.Entryentry=it.next();

sb.append(entry.getKey()+"("+entry.getValue()+")");

}

System.out.println(sb);

}

}

/**

*11、将字符串中进行反转。

abcde-->edcba

*/

publicclasstest5{

publicstaticvoidmain(String[]args){

Stringstr="abcdgrdfgse";

System.out.println(reverse(str));

}

privatestaticStringreverse(Stringstr){

Stringresult="";

char[]c=str.toCharArray();

for(inti=c.length-1;i>=0;i--){

result+=c[i];

}

returnresult;

}

}

/**

*12、

*已知文件a.txt文件中的内容为“bcdeadferwplkou”,请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。

即b

*.txt中的文件内容应为“abcd…………..”这样的顺序。

*/

publicclasstest6{

 

publicstaticvoidmain(String[]args){

FileInputStreamfis=null;

FileOutputStreamfos=null;

try{

fis=newFileInputStream("D:

/a.txt");

fos=newFileOutputStream("D:

/b.txt");

byte[]c=newbyte[fis.available()];

while(fis.read(c)!

=-1){

Arrays.sort(c);

fos.write(c);

}

fos.flush();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}finally{

try{

fis.close();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

try{

fos.close();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

}

/**

*13、编写一个程序,获取10个1至20的随机数,要求随机数不能重复。

*/

publicclasstest7{

publicstaticvoidmain(String[]args){

System.out.println(getRandom());

}

publicstaticListgetRandom(){

Randomrd=newRandom();

ArrayListal=newArrayList();

inti=0;

while(i!

=10){

intr=rd.nextInt(20);

if(!

al.contains(rd)){

al.add(r);

i++;

}

}

returnal;

}

}

/**

*14、编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、售票中心。

售票中心分配一定数量的票,

*由若干个售票窗口进行出售,利用你

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

当前位置:首页 > 表格模板 > 合同协议

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

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