diff --git a/group04/1299310140/src/com/coderising/array/ArrayUtil.java b/group04/1299310140/src/com/coderising/array/ArrayUtil.java index f6de52e0cc..bd7904c5fb 100644 --- a/group04/1299310140/src/com/coderising/array/ArrayUtil.java +++ b/group04/1299310140/src/com/coderising/array/ArrayUtil.java @@ -1,9 +1,8 @@ package com.coderising.array; +import java.util.ArrayList; import java.util.Arrays; -import java.util.List; - -import com.coding.basic.ArrayList; +import java.util.Date; public class ArrayUtil { @@ -34,7 +33,7 @@ public static void reverseArray(int[] origin){ * @return */ public static int[] removeZero(int[] oldArray){ - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList(); for(int i = 0;i < oldArray.length;i++){ if(oldArray[i] != 0){ list.add(oldArray[i]); @@ -52,7 +51,7 @@ public static int[] removeZero(int[] oldArray){ * @return */ public static int[] merge(int[] array1, int[] array2){//参数数组均有序且无重复值 - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList(); int i = 0;//array1 int j = 0;//array2 while(i < array1.length && j < array2.length){ @@ -72,11 +71,13 @@ public static int[] merge(int[] array1, int[] array2){//参数数组均有序且 //此时(i == array1.length && j == array2.length)or //(i == array1.length && j < array2.length)or //(i < array1.length && j == array2.length) - if(i < array1.length){ + while(i < array1.length){ list.add(array1[i]); + i++; } - if(j < array2.length){ + while(j < array2.length){ list.add(array2[j]); + j++; } int[] result = listToArray(list); @@ -139,7 +140,7 @@ public static int[] getPrimes(int max){//max:3 return:[2] if(max <= 2){ return new int[0]; } - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList(); for(int i = 2;i < max;i++){ if(isPrimes(i)){ list.add(i); @@ -149,6 +150,9 @@ public static int[] getPrimes(int max){//max:3 return:[2] return result; } + /* + * 判断一个数是不是质数 + */ public static Boolean isPrimes(int data){ if(data < 2){ return false; @@ -171,7 +175,7 @@ public static Boolean isPrimes(int data){ * @return */ public static int[] getPerfectNumbers(int max){ - ArrayList list = new ArrayList(); + ArrayList list = new ArrayList(); for(int i = 6;i < max;i++){ if(isPerfectNumber(i)){ list.add(i); @@ -181,7 +185,10 @@ public static int[] getPerfectNumbers(int max){ return result; } - public static int[] listToArray(ArrayList list){ + /* + * ArrayList ---> int[] + */ + public static int[] listToArray(ArrayList list){ int[] result = new int[list.size()]; for(int j = 0;j < result.length;j++){ result[j] = (int) list.get(j); @@ -189,6 +196,9 @@ public static int[] listToArray(ArrayList list){ return result; } + /* + * 判断一个数是不是完数 + */ public static Boolean isPerfectNumber(int data){ if(data < 6){ return false; @@ -209,7 +219,7 @@ public static Boolean isPerfectNumber(int data){ return false; } } - } + }//for if(sum == data){ return true; }else{//sum < data @@ -258,8 +268,9 @@ public static String arrayToString(int[] array){ } public static void main(String[] args){ - int[] a1 = getPerfectNumbers(1000000); -// System.out.println(join(a1,"-+-")); + System.out.println(new Date()); + int[] a1 = getPerfectNumbers(2000000); System.out.println(arrayToString(a1)); + System.out.println(new Date()); } } diff --git a/group04/1299310140/src/com/coderising/litestruts/Struts.java b/group04/1299310140/src/com/coderising/litestruts/Struts.java index c4161d6e03..5544cf7fde 100644 --- a/group04/1299310140/src/com/coderising/litestruts/Struts.java +++ b/group04/1299310140/src/com/coderising/litestruts/Struts.java @@ -41,9 +41,13 @@ public static View runAction(String actionName, Map parameters) { */ View view = new View(); + + //0.读取配置文件struts.xml,根据actionName找到相对应的class , 例如LoginAction, Document document = getDocument("src/com/coderising/litestruts/struts.xml"); String className = getAttrValue(document,"/struts/action[@name=\""+actionName+"\"]/@class"); // System.out.println(className); + + //1.通过反射实例化(创建对象) Class cla = null; Object obj = null; Method executeMethod = null; @@ -52,6 +56,8 @@ public static View runAction(String actionName, Map parameters) { obj = cla.newInstance(); //获得该类的所有属性 Field[] fields = cla.getDeclaredFields(); + + //2.根据parameters中的数据,调用对象的setter方法 for(Field field:fields){ if(parameters.get(field.getName()) == null){ continue; @@ -60,16 +66,21 @@ public static View runAction(String actionName, Map parameters) { //获得set方法 Method myset = pd.getWriteMethod(); myset.invoke(obj, parameters.get(field.getName())); -// //获得get方法 -// Method myget = pd.getReadMethod(); -// Object getValue = myget.invoke(obj); -// System.out.println("field:"+field.getName()+"---getValue:"+getValue); } + + //3.通过反射调用对象的exectue 方法, 并获得返回值 executeMethod = cla.getDeclaredMethod("execute"); String executeReturn = (String) executeMethod.invoke(obj); + + //4.通过反射找到对象的所有getter方法,通过反射来调用, 把值和属性形成一个HashMap,放到View对象的parameters Map viewparameters = new HashMap(); for(Field field:fields){ //获得get方法 +// PropertyDescriptor pd = new PropertyDescriptor(field.getName(), cla); +// Method myget = pd.getReadMethod(); +// Object getValue = myget.invoke(obj); +// System.out.println("field:"+field.getName()+"---getValue:"+getValue); + //因为没有setMessage,所以换一种方式实现 Method myget = cla.getDeclaredMethod("get"+captureName(field.getName())); Object getValue = myget.invoke(obj); @@ -77,36 +88,29 @@ public static View runAction(String actionName, Map parameters) { // System.out.println("field:"+field.getName()+"---getValue:"+getValue); } view.setParameters(viewparameters); + + //5.根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp,放到View对象的jsp字段中。 String viewjsp = getTextValue(document,"/struts/action[@name=\""+actionName+"\"]/result[@name=\""+executeReturn+"\"]"); view.setJsp(viewjsp); } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IntrospectionException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { - // TODO Auto-generated catch block e.printStackTrace(); } - return view; } @@ -119,7 +123,6 @@ public static Document getDocument(String file){ try { document = saxReader.read(new File(file)); } catch (DocumentException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return document; @@ -131,24 +134,28 @@ public static Document getDocument(String file){ public static String getAttrValue(Document doc,String attrNode){ List list = doc.selectNodes(attrNode); Iterator iterator = list.iterator(); - while(iterator.hasNext()){ + if(iterator.hasNext()){ Attribute attr = (Attribute) iterator.next(); return attr.getValue(); } - return ""; + + //没有找到给定的属性节点则返回null + return null; } /* * 获取给定xml节点的文本值 */ - public static String getTextValue(Document doc,String attrNode){ - List list = doc.selectNodes(attrNode); + public static String getTextValue(Document doc,String node){ + List list = doc.selectNodes(node); Iterator iterator = list.iterator(); - while(iterator.hasNext()){ + if(iterator.hasNext()){ Element element = (Element) iterator.next(); return element.getText(); } - return ""; + + //没有找到给定的xml节点则返回null + return null; } //首字母大写