黑马入学测试面试题答案(整理)

上传人:第*** 文档编号:32828984 上传时间:2018-02-12 格式:DOC 页数:15 大小:83.50KB
返回 下载 相关 举报
黑马入学测试面试题答案(整理)_第1页
第1页 / 共15页
黑马入学测试面试题答案(整理)_第2页
第2页 / 共15页
黑马入学测试面试题答案(整理)_第3页
第3页 / 共15页
黑马入学测试面试题答案(整理)_第4页
第4页 / 共15页
黑马入学测试面试题答案(整理)_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《黑马入学测试面试题答案(整理)》由会员分享,可在线阅读,更多相关《黑马入学测试面试题答案(整理)(15页珍藏版)》请在金锄头文库上搜索。

1、1.定义一个交通灯枚举,包含红灯、绿灯、黄灯,需要有获得下一个灯的方法;例如:红灯获取下一个灯是绿灯,绿灯获取下一个灯是黄灯。public enum Lamp RED(GREEN),GREEN(YELLOW),YELLOW(RED);private String next;private Lamp(String next)this.next = next;public Lamp nextLamp()return Lamp.valueOf(next); 2、 写一个ArrayList 类的代理,实现和 ArrayList中完全相同的功能,并可以计算每个方法运行的时间。public class t

2、est1 public static void main(String args) final ArrayList target = new ArrayList();List proxy = (List)Proxy.newProxyInstance(List.class.getClassLoader(), ArrayList.class.getInterfaces(), new InvocationHandler() Overridepublic Object invoke(Object proxy, Method method, Object args)throws Throwable lo

3、ng beginTime = System.currentTimeMillis();Thread.sleep(10);Object reVal = method.invoke(target, args);long endTime = System.currentTimeMillis();System.out.println(method.getName()+ runing time is +(endTime-beginTime);return reVal;);proxy.add(nihaoa);proxy.add(nihaoa);proxy.add(nihaoa);proxy.remove(n

4、ihaoa);System.out.println(proxy.toString();3. ArrayList list = new ArrayList();在这个泛型为 Integer 的 ArrayList 中存放一个 String 类型的对象。public class test2 public static void main(String args) throws ExceptionArrayList list = new ArrayList();Method method = list.getClass().getMethod(add, Object.class);method.in

5、voke(list, i am a String);System.out.println(list.toString();4、 一个ArrayList 对象 aList中存有若干个字符串元素,现欲遍历该ArrayList对象,删除其中所有值为abc的字符串元素, 请用代码实现。public class test4 public static void main(String args) ArrayList aList = new ArrayList();aList.add(abc);aList.add(nihaoa);aList.add(nihaoa);aList.add(abc);aList

6、.add(cdb);aList.add(abc);aList.add(cdb);System.out.println(aList.toString();Iterator it = aList.iterator();while(it.hasNext()String str = it.next();if(str.equals(abc)it.remove();System.out.println(aList.toString();5、 编写一个类,增加一个实例方法用于打印一条字符串。并使用反射手段创建该类的对象, 并调用该对象中的方法。public class test5 public static

7、 void main(String args)throws Exception Class clazz = myClass.class;Method method = clazz.getMethod(printStr, String.class);method.invoke(clazz.newInstance(), ni hao ma? this my print str);class myClasspublic void printStr(String str)System.out.println(str);6 、 存在一个JavaBean,它包含以下几种可能的属性: 1:boolean/B

8、oolean 2:int/Integer 3:String4:double/Double 属性名未知,现在要给这些属性设置默认值,以下是要求的默认值: String类型的默认值为 字符串 int/Integer类型的默认值为100 boolean/Boolean类型的默认值为truedouble/Double的默认值为 0.01D.只需要设置带有 getXxx/isXxx/setXxx 方法的属性,非 JavaBean 属性不设置,请用代码实现public class test7 public static void main(String args) throws Exception Cl

9、ass clazz = Class.forName(cn.heima.test.testBean);Object bean = clazz.newInstance();BeanInfo beanInfo = Introspector.getBeanInfo(clazz);/ System.out.println(beanInfo);PropertyDescriptor propertyDescriptors = beanInfo.getPropertyDescriptors();for (PropertyDescriptor pd : propertyDescriptors) / System

10、.out.println(pd);/ 获取属性名Object name = pd.getName();/ 获取属性类型Object type = pd.getPropertyType();/ 获取get方法Method getMethod = pd.getReadMethod();/ 获取set方法Method setMethod = pd.getWriteMethod();if (!class.equals(name) if (setMethod != null) if (type = boolean.class | type = Boolean.class) setMethod.invok

11、e(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, nu

12、ll);class testBean private boolean b;private Integer i;private String str;private Double d;public Boolean getB() return b;public void setB(Boolean b) this.b = b;public Integer getI() return i;public void setI(Integer i) this.i = i;public String getStr() return str;public void setStr(String str) this

13、.str = str;public Double getD() return d;public void setD(Double d) this.d = d;7、 定义一个文件输入流,调用 read(byte b)方法将 exercise.txt 文件中的所有内容打印出来(byte 数组的大小限制为5,不考虑中文编码问题)。public class test8 public static void main(String args) FileInputStream fr = null;try fr = new FileInputStream(d:/exercise.txt);byte bt =

14、 new byte5;int len = 0;while(len = fr.read(bt)!=-1)for (int i = 0; i map = new LinkedHashMap();for (int i = 0; i = 65)|(ci=97&ci it = map.entrySet().iterator();while (it.hasNext() Map.Entry entry = it.next();sb.append(entry.getKey() + ( + entry.getValue() + );System.out.println(sb);/*11、 将字符串中进行反转。a

15、bcde edcba*/public class test5 public static void main(String args) String str = abcdgrdfgse;System.out.println(reverse(str);private static String reverse(String str) String result = ;char c = str.toCharArray();for (int i = c.length-1; i = 0 ; i-) result += ci;return result;/* 12、* 已知文件a.txt文件中的内容为“bcdeadferwplkou”,请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。即b* .txt中的文件内容应为“abcd.”这样的顺序。*/public class test6 public static void main(String args) FileInputStream fis = null;FileOutputStream fos = null;try f

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 工程造价

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号