Skip to content

Commit

Permalink
Merge pull request onlyliuxin#39 from txp-reps/master
Browse files Browse the repository at this point in the history
提交第二次作业
  • Loading branch information
844028312 authored Mar 7, 2017
2 parents 58bb4f3 + 5721095 commit 8a3b557
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1-beta-7</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package com.coderising.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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
* 11 01 11|
*/
public void reverseArray(int[] origin){
int len = origin.length-1;

for (int i = 0; i <((0 == len%2) ? len/2 :(len+1)/2) ; i++) {
origin[i] = origin[i]^origin[len-i];
origin[len-i] = origin[len-i]^origin[i];
origin[i] = origin[i]^origin[len-i];
}
}

/**
* 现在有如下的一个数组: 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){
//get count
int count = 0;
for (int i : oldArray) {
if(0 != i) {
count++;
}
}

//remove zero
int newArray[] = new int[count];
int j=0;
for (int i : oldArray) {
if(0 != i) {
newArray[j++] = i;
}
}
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){

if (null==array1 || null == array2 || 0 == array1.length || 0 == array2.length) {
new IllegalArgumentException("参数不允许为空数组!");
}

int i=0,j=0;
ArrayList<Integer> mergedList = new ArrayList<Integer>();
while (true) {
if (j == array2.length-1 || i == array1.length-1) {
break;
}
if (array1[i] < array2[j]) {
mergedList.add(array1[i]);
i++;
}else if (array1[i] > array2[j]) {
mergedList.add(array2[j]);
j++;
}else {
mergedList.add(array2[j]);
i++;
j++;
}

}

//put the least part of one array into list
if (i == array1.length) {
for (int k = j; k < array2.length; k++) {
mergedList.add(array2[k]);
}
}else {
for (int k = i; k < array1.length; k++) {
mergedList.add(array1[k]);
}
}

return arrayListToInteger(mergedList);
}

private Integer[] arrayListToInteger(ArrayList<Integer> arrayList) {
Integer tempArray[] = new Integer[arrayList.size()];
int i=0;
for (Integer integer : arrayList) {
tempArray[i++] = integer;
}

return tempArray;
}
/**
* 把一个已经存满数据的数组 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){

if (null == oldArray || size<= 0) {
new IllegalArgumentException("数组为空或者长度非法!!");
}

int newArray[] = new int[oldArray.length + size];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
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 ( 1 == max ) {
return new Integer[0];
}
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(1);
arrayList.add(1);

int i=0,j=1;
while (arrayList.get(i)+arrayList.get(j) < max) {
arrayList.add(arrayList.get(i)+arrayList.get(j));
i++;
j++;
}

return arrayListToInteger(arrayList);
}

/**
* 返回小于给定最大值max的所有素数数组
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
* @param max
* @return
*/
public Integer[] getPrimes(int max){
ArrayList<Integer> arrayList = new ArrayList<Integer>();
boolean isPrimes = true;
for (int i = 2; i < max; i++) {
isPrimes = true;
for (int j = 2; j <= Math.sqrt(i) ; j++) {
if (0 == i % j) {
isPrimes = false;
break;
}
}

if (isPrimes) {
arrayList.add(i);
}

}
return arrayListToInteger(arrayList);
}

/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
* @param max
* @return
*/
public Integer[] getPerfectNumbers(int max){
ArrayList<Integer> arrayList = new ArrayList<Integer>();
int sum = 0;
for (int i = 2; i < max; i++) {
sum = 0;
//the factor is less than half of a number
for (int j = 1; j <= (i+1)/2; j++) {
if (0 == i%j) {
sum += j;
}
}

if (sum == i) {
arrayList.add(i);
}
}
return arrayListToInteger(arrayList);
}

/**
* 用seperator 把数组 array给连接起来
* 例如array= [3,8,9], seperator = "-"
* 则返回值为"3-8-9"
* @param array
* @param s
* @return
*/
public String join(int[] array, String seperator){
StringBuilder sb = new StringBuilder();
for (int i : array) {
sb.append(i).append(seperator);
}

sb.deleteCharAt(sb.length()-1);
return sb.toString();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.coderising.litestruts;

/**
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
* @author liuxin
*
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.coderising.litestruts;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;



public class Struts {

public static View runAction(String actionName, Map<String,String> parameters) {


//0. 读取配置文件struts.xml
Document xmlDoc = null;
View view = null;
try {
xmlDoc = getXMLDocument("./src/main/java/com/coderising/litestruts/struts.xml");

/*1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)
据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是
("name"="test" , "password"="1234") ,
那就应该调用 setName和setPassword方法*/

String classStr = xmlDoc.selectSingleNode("struts/action[@name='"+actionName+"']/@class").getText();
Class<?> actionClass = Class.forName(classStr);
Object action = actionClass.newInstance();

Iterator<Map.Entry<String,String>> itr = parameters.entrySet().iterator();
Entry<String,String> entry = null;
String key = "";
String value = "";
while(itr.hasNext()) {
entry = itr.next();
key = entry.getKey();
value = entry.getValue();
Method setter = actionClass.getMethod("set"+key.substring(0, 1).toUpperCase()+key.substring(1),String.class);
setter.invoke(action, value);
}


// 2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"
Method exeMethod = actionClass.getMethod("execute");
String result = exeMethod.invoke(action).toString();
// System.out.println(result);

/*3. 通过反射找到对象的所有getter方法(例如 getMessage),
通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} ,
放到View对象的parameters*/

Map<String,String> viewDataMap = new HashMap<String,String>();
String methodName = "";
for (Method md : actionClass.getMethods()) {
methodName = md.getName();
if (methodName.startsWith("get")) {
viewDataMap.put(methodName.substring(3, 4).toLowerCase()+methodName.substring(4), md.invoke(action).toString());
}
}

// System.out.println(viewDataMap);

/**
4. 根据struts.xml中的 <result> 配置,以及execute的返回值, 确定哪一个jsp,
放到View对象的jsp字段中。
*/
String jspRet = xmlDoc.selectSingleNode("struts/action[@name='"+actionName+"']/result[@name='"+result+"']").getText();
view = new View();
view.setJsp(jspRet);
view.setParameters(viewDataMap);
} catch (Exception e) {
e.printStackTrace();
}
return view;
}

private static Document getXMLDocument(String filePath) throws DocumentException {
return new SAXReader().read(filePath);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.coderising.litestruts;

import java.util.Map;

public class View {
private String jsp;
private Map parameters;

public String getJsp() {
return jsp;
}
public View setJsp(String jsp) {
this.jsp = jsp;
return this;
}
public Map getParameters() {
return parameters;
}
public View setParameters(Map parameters) {
this.parameters = parameters;
return this;
}
}
Loading

0 comments on commit 8a3b557

Please sign in to comment.