diff --git a/group04/1299310140/src/com/coderising/array/ArrayUtil.java b/group04/1299310140/src/com/coderising/array/ArrayUtil.java new file mode 100644 index 0000000000..f6de52e0cc --- /dev/null +++ b/group04/1299310140/src/com/coderising/array/ArrayUtil.java @@ -0,0 +1,265 @@ +package com.coderising.array; + +import java.util.Arrays; +import java.util.List; + +import com.coding.basic.ArrayList; + +public class ArrayUtil { + + /** + * 给定一个整形数组a , 对该数组的值进行置换 + 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] + 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7] + * @param origin + * @return + */ + public static void reverseArray(int[] origin){ + int temp; + for(int i = 0;i < origin.length;i++){ + if(i >= origin.length-1-i){ + break; + } + temp = origin[i]; + origin[i] = origin[origin.length-1-i]; + origin[origin.length-1-i] = temp; + } + } + + /** + * 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} + * 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: + * {1,3,4,5,6,6,5,4,7,6,7,5} + * @param oldArray + * @return + */ + public static int[] removeZero(int[] oldArray){ + ArrayList list = new ArrayList(); + for(int i = 0;i < oldArray.length;i++){ + if(oldArray[i] != 0){ + list.add(oldArray[i]); + } + } + int[] result = listToArray(list); + return result; + } + + /** + * 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 + * 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复 + * @param array1 + * @param array2 + * @return + */ + public static int[] merge(int[] array1, int[] array2){//参数数组均有序且无重复值 + ArrayList list = new ArrayList(); + int i = 0;//array1 + int j = 0;//array2 + while(i < array1.length && j < array2.length){ + if(array1[i] == array2[j]){ + list.add(array1[i]); + i++; + j++; + }else if(array1[i] < array2[j]){ + list.add(array1[i]); + i++; + }else{//array1[i] > array2[j] + list.add(array2[j]); + j++; + } + }//while结束 + + //此时(i == array1.length && j == array2.length)or + //(i == array1.length && j < array2.length)or + //(i < array1.length && j == array2.length) + if(i < array1.length){ + list.add(array1[i]); + } + if(j < array2.length){ + list.add(array2[j]); + } + + int[] result = listToArray(list); + return result; + } + + /** + * 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size + * 注意,老数组的元素在新数组中需要保持 + * 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 + * [2,3,6,0,0,0] + * @param oldArray + * @param size + * @return + */ + public static int[] grow(int [] oldArray, int size){ + return Arrays.copyOf(oldArray, oldArray.length + size); +// int[] result = new int[oldArray.length + size]; +// System.arraycopy(oldArray, 0, result, 0, oldArray.length); +// return result; + } + + /** + * 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 + * 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] + * max = 1, 则返回空数组 [] + * @param max + * @return + */ + public static int[] fibonacci(int max){ + if(max < 2){//max=1 特殊 + return new int[0]; + } + int one = 1; + int two = 1; + int temp = 0; + int i = 2; + while(one + two < max){ + temp = two; + two = one + two; + one = temp; + i++; + } + int[] result = new int[i]; + result[0] = 1; + result[1] = 1; + for(int j = 2;j < result.length;j++){ + result[j] = result[j-1] + result[j-2]; + } + return result; + } + + /** + * 返回小于给定最大值max的所有素数数组 + * 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] + * @param max + * @return + */ + public static int[] getPrimes(int max){//max:3 return:[2] + if(max <= 2){ + return new int[0]; + } + ArrayList list = new ArrayList(); + for(int i = 2;i < max;i++){ + if(isPrimes(i)){ + list.add(i); + } + } + int[] result = listToArray(list); + return result; + } + + public static Boolean isPrimes(int data){ + if(data < 2){ + return false; + } + for(int i = 2;i < data;i++){ + if(data % i == 0){ + return false; + } + if(i * i > data){ + break; + } + } + return true; + } + + /** + * 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 + * 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 + * @param max + * @return + */ + public static int[] getPerfectNumbers(int max){ + ArrayList list = new ArrayList(); + for(int i = 6;i < max;i++){ + if(isPerfectNumber(i)){ + list.add(i); + } + } + int[] result = listToArray(list); + return result; + } + + 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); + } + return result; + } + + public static Boolean isPerfectNumber(int data){ + if(data < 6){ + return false; + } + int sum = 1; + for(int i = 2;i < data;i++){ + if(i * i > data){ + break; + } + if(data % i == 0){ + if(i == data/i){ + sum = sum + i; + }else{ + sum = sum + i + data/i; + } +// sum = sum + i; + if(sum > data){ + return false; + } + } + } + if(sum == data){ + return true; + }else{//sum < data + return false; + } + } + + /** + * 用seperator 把数组 array给连接起来 + * 例如array= [3,8,9], seperator = "-" + * 则返回值为"3-8-9" + * @param array + * @param s + * @return + */ + public static String join(int[] array, String seperator){ + StringBuffer result = new StringBuffer(); + for(int i = 0;i < array.length;i++){ + result.append(array[i]); + result.append(seperator); + } + + String res = result.toString(); + if(array.length > 0){ + res = res.substring(0, result.length()-seperator.length());//删除最后一个seperator + } + return res; + } + + /* + * 数组转字符串 格式:[]or[1]or[1 2] + */ + public static String arrayToString(int[] array){ + StringBuffer result = new StringBuffer(); + result.append("["); + for(int i = 0;i < array.length;i++){ + result.append(array[i]); + result.append(" "); + } + String res = result.toString(); + if(array.length > 0){ + res = res.substring(0, result.length()-1);//删除最后一个空格 + } + res = res + "]"; + return res; + } + + public static void main(String[] args){ + int[] a1 = getPerfectNumbers(1000000); +// System.out.println(join(a1,"-+-")); + System.out.println(arrayToString(a1)); + } +} diff --git a/group04/1299310140/src/com/coderising/litestruts/Struts.java b/group04/1299310140/src/com/coderising/litestruts/Struts.java new file mode 100644 index 0000000000..c4161d6e03 --- /dev/null +++ b/group04/1299310140/src/com/coderising/litestruts/Struts.java @@ -0,0 +1,173 @@ +package com.coderising.litestruts; + +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.dom4j.Attribute; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; + +public class Struts { + + public static View runAction(String actionName, Map parameters) { + + /* + + 0. 读取配置文件struts.xml + + 1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象) + 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 + ("name"="test" , "password"="1234") , + 那就应该调用 setName和setPassword方法 + + 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" + + 3. 通过反射找到对象的所有getter方法(例如 getMessage), + 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , + 放到View对象的parameters + + 4. 根据struts.xml中的 配置,以及execute的返回值, 确定哪一个jsp, + 放到View对象的jsp字段中。 + + */ + View view = new View(); + Document document = getDocument("src/com/coderising/litestruts/struts.xml"); + String className = getAttrValue(document,"/struts/action[@name=\""+actionName+"\"]/@class"); +// System.out.println(className); + Class cla = null; + Object obj = null; + Method executeMethod = null; + try { + cla = Class.forName(className); + obj = cla.newInstance(); + //获得该类的所有属性 + Field[] fields = cla.getDeclaredFields(); + for(Field field:fields){ + if(parameters.get(field.getName()) == null){ + continue; + } + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), cla); + //获得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); + } + executeMethod = cla.getDeclaredMethod("execute"); + String executeReturn = (String) executeMethod.invoke(obj); + Map viewparameters = new HashMap(); + for(Field field:fields){ + //获得get方法 + //因为没有setMessage,所以换一种方式实现 + Method myget = cla.getDeclaredMethod("get"+captureName(field.getName())); + Object getValue = myget.invoke(obj); + viewparameters.put(field.getName(), (String) getValue); +// System.out.println("field:"+field.getName()+"---getValue:"+getValue); + } + view.setParameters(viewparameters); + 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; + } + + /* + * 根据xml文件的路径获取其Document + */ + public static Document getDocument(String file){ + SAXReader saxReader = new SAXReader(); + Document document = null; + try { + document = saxReader.read(new File(file)); + } catch (DocumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return document; + } + + /* + * 获取给定属性节点的值 + */ + public static String getAttrValue(Document doc,String attrNode){ + List list = doc.selectNodes(attrNode); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + Attribute attr = (Attribute) iterator.next(); + return attr.getValue(); + } + return ""; + } + + /* + * 获取给定xml节点的文本值 + */ + public static String getTextValue(Document doc,String attrNode){ + List list = doc.selectNodes(attrNode); + Iterator iterator = list.iterator(); + while(iterator.hasNext()){ + Element element = (Element) iterator.next(); + return element.getText(); + } + return ""; + } + + //首字母大写 + public static String captureName(String name) { +// name = name.substring(0, 1).toUpperCase() + name.substring(1); +// return name; + char[] cs=name.toCharArray(); + cs[0]-=32; + return String.valueOf(cs); + } + + public static void main(String[] args){ + String actionName = "login"; + + Map params = new HashMap(); + params.put("name","test"); + params.put("password","1234"); + + runAction(actionName,params); + } + +}