Skip to content

Commit f80f2b5

Browse files
committed
为ArrayList实现toArray()方法,添加DocumentUtil类
1 parent 63bca02 commit f80f2b5

File tree

11 files changed

+335
-64
lines changed

11 files changed

+335
-64
lines changed

group20/592146505/array_util/src/org/wsc/array/ArrayUtil.java

+43-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.wsc.array;
22

3-
import java.util.Arrays;
43

54
public class ArrayUtil {
65
/**
@@ -10,7 +9,7 @@ public class ArrayUtil {
109
* @param origin
1110
* @return
1211
*/
13-
public void reverseArray(int[] origin){
12+
public static void reverseArray(int[] origin){
1413
//折半
1514
for (int i = 0; i < (origin.length >> 1); i++) {
1615
int num = origin[i];
@@ -26,7 +25,7 @@ public void reverseArray(int[] origin){
2625
* @param oldArray
2726
* @return
2827
*/
29-
public int[] removeZero(int[] oldArray){
28+
public static int[] removeZero(int[] oldArray){
3029
int count = 0;//计数器
3130
/*
3231
* 利用冒泡,将0元素向后排
@@ -53,6 +52,30 @@ public int[] removeZero(int[] oldArray){
5352
return newArray;
5453
}
5554

55+
/**
56+
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
57+
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
58+
* {1,3,4,5,6,6,5,4,7,6,7,5}
59+
* @param oldArray
60+
* @return
61+
*/
62+
public static int[] removeZero2(int[] oldArray){
63+
int count = 0;//计数器
64+
for (int i = 0; i < oldArray.length; i++) {
65+
if(oldArray[i] == 0)
66+
count++;//计数器+1
67+
}
68+
//创建新数组
69+
int[] newArray = new int[oldArray.length-count];
70+
for (int i = 0,j=0; i < oldArray.length; i++) {
71+
if(oldArray[i] != 0){
72+
newArray[j] = oldArray[i];
73+
j++;
74+
}
75+
}
76+
return newArray;
77+
}
78+
5679
/**
5780
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的
5881
* 例如 a1 = [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
@@ -61,7 +84,7 @@ public int[] removeZero(int[] oldArray){
6184
* @return
6285
*/
6386

64-
public int[] merge(int[] array1, int[] array2){
87+
public static int[] merge(int[] array1, int[] array2){
6588
return null;
6689
}
6790
/**
@@ -73,8 +96,12 @@ public int[] merge(int[] array1, int[] array2){
7396
* @param size
7497
* @return
7598
*/
76-
public int[] grow(int [] oldArray, int size){
77-
return null;
99+
public static int[] grow(int [] oldArray, int size){
100+
int[] newArray = new int[oldArray.length+size];
101+
for (int i = 0; i < oldArray.length; i++) {
102+
newArray[i] = oldArray[i];
103+
}
104+
return newArray;
78105
}
79106

80107
/**
@@ -84,7 +111,7 @@ public int[] grow(int [] oldArray, int size){
84111
* @param max
85112
* @return
86113
*/
87-
public int[] fibonacci(int max){
114+
public static int[] fibonacci(int max){
88115
return null;
89116
}
90117

@@ -94,7 +121,7 @@ public int[] fibonacci(int max){
94121
* @param max
95122
* @return
96123
*/
97-
public int[] getPrimes(int max){
124+
public static int[] getPrimes(int max){
98125
return null;
99126
}
100127

@@ -104,7 +131,7 @@ public int[] getPrimes(int max){
104131
* @param max
105132
* @return
106133
*/
107-
public int[] getPerfectNumbers(int max){
134+
public static int[] getPerfectNumbers(int max){
108135
return null;
109136
}
110137

@@ -116,7 +143,12 @@ public int[] getPerfectNumbers(int max){
116143
* @param s
117144
* @return
118145
*/
119-
public String join(int[] array, String seperator){
120-
return null;
146+
public static String join(int[] array, String seperator){
147+
String str = "";
148+
for (int i = 0; i < array.length; i++) {
149+
str += i != array.length -1 ?
150+
array[i] + seperator : array[i];
151+
}
152+
return str;
121153
}
122154
}

group20/592146505/array_util/src/org/wsc/array/ArrayUtilTest.java

+31-28
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,43 @@
22

33
import static org.junit.Assert.*;
44

5-
import org.junit.After;
6-
import org.junit.Before;
75
import org.junit.Test;
86

97
public class ArrayUtilTest {
10-
ArrayUtil arrayUtil;
11-
@Before
12-
public void setUp() throws Exception {
13-
arrayUtil = new ArrayUtil();
14-
}
15-
16-
@After
17-
public void tearDown() throws Exception {
18-
arrayUtil = null;
19-
}
208

21-
@Test
22-
public void testReverseArray() {
23-
int[] nums = new int[]{7, 9 , 30, 3, 5};
24-
arrayUtil.reverseArray(nums);
25-
for (int i = 0; i < nums.length; i++) {
26-
System.out.println(nums[i]);
27-
}
28-
fail("Not yet implemented");
29-
}
9+
// @Test
10+
// public void testReverseArray() {
11+
// int[] nums = new int[]{7, 9 , 30, 3, 5};
12+
// ArrayUtil.reverseArray(nums);
13+
// for (int i = 0; i < nums.length; i++) {
14+
// System.out.println(nums[i]);
15+
// }
16+
// fail("Not yet implemented");
17+
// }
18+
//
19+
// @Test
20+
// public void testRemoveZero() {
21+
// int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0};
22+
// nums = ArrayUtil.removeZero2(nums);
23+
// for (int i = 0; i < nums.length; i++) {
24+
// System.out.println(nums[i]);
25+
// }
26+
// fail("Not yet implemented");
27+
// }
28+
//
29+
// @Test
30+
// public void testJoin() {
31+
// int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0};
32+
// String str = ArrayUtil.join(nums,"-");
33+
// System.out.println(str);
34+
// fail("Not yet implemented");
35+
// }
3036

3137
@Test
32-
public void testRemoveZero() {
33-
int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5,0};
34-
nums = arrayUtil.removeZero(nums);
35-
for (int i = 0; i < nums.length; i++) {
36-
System.out.println(nums[i]);
37-
}
38-
fail("Not yet implemented");
38+
public void testGrow() {
39+
int[] nums = new int[]{0,7, 9 , 0,0,30,0, 3, 5};
40+
nums = ArrayUtil.grow(nums,3);
41+
assertTrue(nums.length==12);
3942
}
4043

4144
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.wsc.litestruts;
2+
3+
/**
4+
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
5+
* @author liuxin
6+
*
7+
*/
8+
public class LoginAction{
9+
private String name ;
10+
private String password;
11+
private String message;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public String getPassword() {
18+
return password;
19+
}
20+
21+
public String execute(){
22+
if("test".equals(name) && "1234".equals(password)){
23+
this.message = "login successful";
24+
return "success";
25+
}
26+
this.message = "login failed,please check your user/pwd";
27+
return "fail";
28+
}
29+
30+
public void setName(String name){
31+
this.name = name;
32+
}
33+
public void setPassword(String password){
34+
this.password = password;
35+
}
36+
public String getMessage(){
37+
return this.message;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.wsc.litestruts;
2+
3+
import java.util.Map;
4+
5+
6+
public class Struts {
7+
8+
public static View runAction(String actionName, Map<String,String> parameters) {
9+
10+
11+
/*
12+
13+
0. 读取配置文件struts.xml
14+
15+
1. 根据actionName找到相对应的class , 例如LoginAction, 通过反射实例化(创建对象)
16+
据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是
17+
("name"="test" , "password"="1234") ,
18+
那就应该调用 setName和setPassword方法
19+
20+
2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"
21+
22+
3. 通过反射找到对象的所有getter方法(例如 getMessage),
23+
通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message": "登录成功"} ,
24+
放到View对象的parameters
25+
26+
4. 根据struts.xml中的 <result> 配置,以及execute的返回值, 确定哪一个jsp,
27+
放到View对象的jsp字段中。
28+
29+
*/
30+
31+
return null;
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.wsc.litestruts;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
10+
11+
12+
13+
public class StrutsTest {
14+
15+
@Test
16+
public void testLoginActionSuccess() {
17+
18+
String actionName = "login";
19+
20+
Map<String,String> params = new HashMap<String,String>();
21+
params.put("name","test");
22+
params.put("password","1234");
23+
24+
25+
View view = Struts.runAction(actionName,params);
26+
27+
Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
28+
Assert.assertEquals("login successful", view.getParameters().get("message"));
29+
}
30+
31+
@Test
32+
public void testLoginActionFailed() {
33+
String actionName = "login";
34+
Map<String,String> params = new HashMap<String,String>();
35+
params.put("name","test");
36+
params.put("password","123456"); //密码和预设的不一致
37+
38+
View view = Struts.runAction(actionName,params);
39+
40+
Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp());
41+
Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message"));
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.wsc.litestruts;
2+
3+
import java.util.Map;
4+
5+
public class View {
6+
private String jsp;
7+
private Map<String, String> parameters;
8+
9+
public String getJsp() {
10+
return jsp;
11+
}
12+
13+
public View setJsp(String jsp) {
14+
this.jsp = jsp;
15+
return this;
16+
}
17+
18+
public Map<String, String> getParameters() {
19+
return parameters;
20+
}
21+
22+
public View setParameters(Map<String, String> parameters) {
23+
this.parameters = parameters;
24+
return this;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<struts>
3+
<action name="login" class="com.coderising.action.LoginAction">
4+
<result name="success">/jsp/homepage.jsp</result>
5+
<result name="fail">/jsp/showLogin.jsp</result>
6+
</action>
7+
<action name="logout" class="com.coderising.action.LogoutAction">
8+
<result name = "success">/jsp/welcome.jsp</result>
9+
<result name = "error">/jsp/error.jsp</result>
10+
</action>
11+
</struts>

0 commit comments

Comments
 (0)