当前位置:文档之家› 黑马入学测试题答案(整理).(DOC)

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

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 test1 {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 {long 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("nihaoa");System.out.println(proxy.toString());}}3. ArrayList list = new ArrayList();在这个泛型为Integer的ArrayList中存放一个String类型的对象。

public class test2 {public static void main(String[] args) throws Exception{ ArrayList<Integer> list = new ArrayList<Integer>();Method method = list.getClass().getMethod("add", Object.class);method.invoke(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<String> aList = new ArrayList<String>();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());Iterator<String> 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 void main(String[] args)throws Exception { Class<myClass> clazz = myClass.class;Method method = clazz.getMethod("printStr", String.class);method.invoke(clazz.newInstance(), "ni hao ma? this my print str");}}class myClass{public void printStr(String str){System.out.println(str);}}6 、存在一个JavaBean,它包含以下几种可能的属性: 1:boolean/Boolean 2:int/Integer3: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 { Class 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.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.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));}}}}}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.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 = new byte[5];int len = 0;while((len = fr.read(bt))!=-1){for (int i = 0; i < len; i++) {System.out.print((char)bt[i]);}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fr!=null){try {fr.close();} catch (IOException e) {e.printStackTrace();}finally{fr = null;}}}}}8、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。

相关主题