Skip to content

Commit

Permalink
check
Browse files Browse the repository at this point in the history
  • Loading branch information
'1299310140' committed Mar 3, 2017
1 parent c87ac10 commit e933d6a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
37 changes: 24 additions & 13 deletions group04/1299310140/src/com/coderising/array/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -34,7 +33,7 @@ public static void reverseArray(int[] origin){
* @return
*/
public static int[] removeZero(int[] oldArray){
ArrayList list = new ArrayList();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0;i < oldArray.length;i++){
if(oldArray[i] != 0){
list.add(oldArray[i]);
Expand All @@ -52,7 +51,7 @@ public static int[] removeZero(int[] oldArray){
* @return
*/
public static int[] merge(int[] array1, int[] array2){//参数数组均有序且无重复值
ArrayList list = new ArrayList();
ArrayList<Integer> list = new ArrayList<Integer>();
int i = 0;//array1
int j = 0;//array2
while(i < array1.length && j < array2.length){
Expand All @@ -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);
Expand Down Expand Up @@ -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<Integer> list = new ArrayList<Integer>();
for(int i = 2;i < max;i++){
if(isPrimes(i)){
list.add(i);
Expand All @@ -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;
Expand All @@ -171,7 +175,7 @@ public static Boolean isPrimes(int data){
* @return
*/
public static int[] getPerfectNumbers(int max){
ArrayList list = new ArrayList();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 6;i < max;i++){
if(isPerfectNumber(i)){
list.add(i);
Expand All @@ -181,14 +185,20 @@ public static int[] getPerfectNumbers(int max){
return result;
}

public static int[] listToArray(ArrayList list){
/*
* ArrayList<Integer> ---> int[]
*/
public static int[] listToArray(ArrayList<Integer> 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;
Expand All @@ -209,7 +219,7 @@ public static Boolean isPerfectNumber(int data){
return false;
}
}
}
}//for
if(sum == data){
return true;
}else{//sum < data
Expand Down Expand Up @@ -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());
}
}
47 changes: 27 additions & 20 deletions group04/1299310140/src/com/coderising/litestruts/Struts.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ public static View runAction(String actionName, Map<String,String> 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;
Expand All @@ -52,6 +56,8 @@ public static View runAction(String actionName, Map<String,String> parameters) {
obj = cla.newInstance();
//获得该类的所有属性
Field[] fields = cla.getDeclaredFields();

//2.根据parameters中的数据,调用对象的setter方法
for(Field field:fields){
if(parameters.get(field.getName()) == null){
continue;
Expand All @@ -60,53 +66,51 @@ public static View runAction(String actionName, Map<String,String> 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<String,String> viewparameters = new HashMap<String,String>();
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);
viewparameters.put(field.getName(), (String) getValue);
// System.out.println("field:"+field.getName()+"---getValue:"+getValue);
}
view.setParameters(viewparameters);

//5.根据struts.xml中的 <result> 配置,以及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;
}

Expand All @@ -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;
Expand All @@ -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;
}

//首字母大写
Expand Down

0 comments on commit e933d6a

Please sign in to comment.