forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from vxzh/master
L2
- Loading branch information
Showing
6 changed files
with
445 additions
and
0 deletions.
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
group13/2729382520/L2/src/io/github/vxzh/struts/ArrayUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package io.github.vxzh.struts; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by vxzh on 28/02/2017. | ||
*/ | ||
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 void reverseArray(int[] origin) { | ||
for (int i = 0; i < (origin.length >> 1); i++) { | ||
int 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 int[] removeZero(int[] oldArray) { | ||
int count = 0; | ||
for (int i = 0; i < oldArray.length; i++) { | ||
if (oldArray[i] != 0) { | ||
count++; | ||
} | ||
} | ||
|
||
int[] newArray = new int[count]; | ||
int j = 0; | ||
for (int i = 0; i < oldArray.length; i++) { | ||
if (oldArray[i] != 2) { | ||
newArray[j] = oldArray[i]; | ||
j++; | ||
} | ||
} | ||
|
||
return newArray; | ||
} | ||
|
||
/** | ||
* 给定两个已经排序好的整形数组, 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 Integer[] merge(int[] array1, int[] array2) { | ||
|
||
List<Integer> result = new ArrayList<Integer>(); | ||
int i = 0, j = 0; | ||
while (i < array1.length && j < array2.length) { | ||
if (i != array1.length - 1 && array1[i] == array1[i + 1]) { | ||
i++; | ||
continue; | ||
} | ||
if (j != array2.length - 1 && array2[j] == array2[j + 1]) { | ||
j++; | ||
continue; | ||
} | ||
if (array1[i] > array2[j]) { | ||
result.add(array2[j++]); | ||
} else if (array1[i] < array2[j]) { | ||
result.add(array1[i++]); | ||
} else if (array1[i] == array2[j]) { | ||
result.add(array1[i]); | ||
i++; | ||
j++; | ||
} | ||
} | ||
while (i < array1.length) { | ||
result.add(array1[i++]); | ||
} | ||
while (j < array2.length) { | ||
result.add(array2[j++]); | ||
} | ||
return result.toArray(new Integer[result.size()]); | ||
} | ||
|
||
/** | ||
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size | ||
* 注意,老数组的元素在新数组中需要保持 | ||
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为 | ||
* [2,3,6,0,0,0] | ||
* | ||
* @param oldArray | ||
* @param size | ||
* @return | ||
*/ | ||
public int[] grow(int[] oldArray, int size) { | ||
int[] newArray = new int[oldArray.length + size]; | ||
for (int i = 0; i < oldArray.length; i++) { | ||
newArray[i] = oldArray[i]; | ||
} | ||
return newArray; | ||
} | ||
|
||
/** | ||
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 | ||
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13] | ||
* max = 1, 则返回空数组 [] | ||
* | ||
* @param max | ||
* @return | ||
*/ | ||
public Integer[] fibonacci(int max) { | ||
if (max <= 1) | ||
return new Integer[0]; | ||
List<Integer> list = new ArrayList<Integer>(); | ||
list.add(1); | ||
list.add(1); | ||
int last = 1; | ||
int count = list.size(); | ||
while (last < max) { | ||
int x = list.get(count - 1) + list.get(count - 2); | ||
list.add(x); | ||
count++; | ||
last = x; | ||
} | ||
|
||
list.remove(count - 1); | ||
return list.toArray(new Integer[list.size()]); | ||
} | ||
|
||
/** | ||
* 返回小于给定最大值max的所有素数数组 | ||
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19] | ||
* | ||
* @param max | ||
* @return | ||
*/ | ||
public Integer[] getPrimes(int max) { | ||
List<Integer> list = new ArrayList<Integer>(); | ||
if (max < 2) | ||
return new Integer[0]; | ||
else if (max == 2) | ||
return new Integer[]{2}; | ||
else { | ||
list.add(2); | ||
for (int i = 3; i <= max; i += 2) { | ||
boolean flag = true; | ||
for (int j = 2; j < i; j++) { | ||
if (i % j == 0) { | ||
flag = false; | ||
} | ||
} | ||
if (flag) { | ||
list.add(i); | ||
} | ||
} | ||
if (list.get(list.size() - 1) >= max) | ||
list.remove(list.size() - 1); | ||
} | ||
|
||
return list.toArray(new Integer[list.size()]); | ||
} | ||
|
||
/** | ||
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 | ||
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数 | ||
* | ||
* @param max | ||
* @return | ||
*/ | ||
public Integer[] getPerfectNumbers(int max) { | ||
List<Integer> list = new ArrayList<Integer>(); | ||
for (int i = 1; i <= max; i++) { | ||
int sum = 0; | ||
for (int j = 1; j < i; j++) { | ||
if (i % j == 0) { | ||
sum += j; | ||
} | ||
} | ||
if (sum == i) { | ||
System.out.println(i); | ||
list.add(i); | ||
} | ||
} | ||
|
||
return list.toArray(new Integer[list.size()]); | ||
} | ||
|
||
/** | ||
* 用seperator 把数组 array给连接起来 | ||
* 例如array= [3,8,9], seperator = "-" | ||
* 则返回值为"3-8-9" | ||
* | ||
* @param array | ||
* @param seperator | ||
* @return | ||
*/ | ||
public String join(int[] array, String seperator) { | ||
StringBuilder builder = new StringBuilder(); | ||
for (int i = 0; i < array.length; i++) { | ||
if (i == array.length - 1) | ||
builder.append(array[i]); | ||
else | ||
builder.append(array[i]).append(seperator); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
group13/2729382520/L2/src/io/github/vxzh/struts/LoginAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.vxzh.struts; | ||
|
||
|
||
/** | ||
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。 | ||
*/ | ||
public class LoginAction { | ||
private String name; | ||
private String password; | ||
private String message; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public String execute() { | ||
if ("test".equals(name) && "1234".equals(password)) { | ||
this.message = "login successful"; | ||
return "success"; | ||
} | ||
this.message = "login failed,please check your user/pwd"; | ||
return "fail"; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public String getMessage() { | ||
return this.message; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
group13/2729382520/L2/src/io/github/vxzh/struts/Struts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.github.vxzh.struts; | ||
|
||
|
||
import org.dom4j.Document; | ||
import org.dom4j.DocumentException; | ||
import org.dom4j.Element; | ||
import org.dom4j.io.SAXReader; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by vxzh on 28/02/2017. | ||
*/ | ||
public class Struts { | ||
|
||
public static View runAction(String actionName, Map<String, String> parameters) | ||
throws DocumentException, ClassNotFoundException, IllegalAccessException, | ||
InstantiationException, NoSuchMethodException, InvocationTargetException { | ||
|
||
Map<String, String> map = parseXML(actionName, "/Users/zzzz/Documents/IdeaProjects/demo/src/io/github/vxzh/struts/struts.xml"); | ||
|
||
|
||
/** | ||
* 1. 根据actionName找到相对应的class, 例如LoginAction, 通过反射实例化(创建对象) | ||
* 据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 | ||
* ("name"="test" ,"password"="1234") ,那就应该调用setName和setPassword方法 | ||
*/ | ||
Class clz = Class.forName(map.get(actionName)); | ||
LoginAction loginAction = (LoginAction) clz.newInstance(); | ||
Method name = clz.getMethod("setName", String.class); | ||
Method password = clz.getMethod("setPassword", String.class); | ||
name.invoke(loginAction, parameters.get("name")); | ||
password.invoke(loginAction, parameters.get("password")); | ||
|
||
/** | ||
* 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success" | ||
*/ | ||
Method exectue = clz.getMethod("execute"); | ||
String str = (String) exectue.invoke(loginAction); | ||
map.put("returnValue", str); | ||
|
||
/** | ||
* 3. 通过反射找到对象的所有getter方法(例如 getMessage), | ||
* 通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} , | ||
* 放到View对象的parameters | ||
*/ | ||
Method getMessage = clz.getMethod("getMessage"); | ||
String message = (String) getMessage.invoke(loginAction); | ||
Map<String, String> hashMap = new HashMap<>(); | ||
hashMap.put("message", message); | ||
|
||
|
||
/** | ||
* 4. 根据struts.xml中的 <result> 配置, 以及execute的返回值, 确定哪一个jsp, | ||
* 放到View对象的jsp字段中。 | ||
*/ | ||
View view = new View(); | ||
view.setParameters(hashMap); | ||
String jsp = map.get(map.get("returnValue")); | ||
view.setJsp(jsp); | ||
return view; | ||
} | ||
|
||
public static Map<String, String> parseXML(String actionName, String path) throws DocumentException { | ||
SAXReader saxReader = new SAXReader(); | ||
File file = new File(path); | ||
Document document = saxReader.read(file); | ||
|
||
//获取根节点对象 | ||
Element root = document.getRootElement(); | ||
Element actionElement = null; | ||
HashMap<String, String> map = new HashMap<>(); | ||
for (Iterator i = root.elementIterator(); i.hasNext(); ) { | ||
Element element = (Element) i.next(); | ||
String name = element.attributeValue("name"); | ||
String clazz = element.attributeValue("class"); | ||
if (actionName.equals(name)) { | ||
actionElement = element; | ||
map.put(name, clazz); | ||
} | ||
} | ||
|
||
for (Iterator j = actionElement.elementIterator(); j.hasNext(); ) { | ||
Element elem = (Element) j.next(); | ||
String resultName = elem.attributeValue("name"); | ||
String jsp = elem.getText(); | ||
map.put(resultName, jsp); | ||
} | ||
|
||
return map; | ||
} | ||
|
||
} |
Oops, something went wrong.