Skip to content

Commit 5618b00

Browse files
committed
差merge
1 parent 6daa87d commit 5618b00

File tree

8 files changed

+588
-0
lines changed

8 files changed

+588
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package assignment2_26;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class ArrayUtil {
8+
9+
/**
10+
* 给定一个整形数组a , 对该数组的值进行置换 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7] 如果 a =
11+
* [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
12+
*
13+
* @param origin
14+
* @return
15+
*/
16+
public void reverseArray(int[] origin) {
17+
int mid = origin.length / 2;
18+
for (int i = 0; i < mid; i++) {
19+
int temp = origin[i];
20+
int reversePosition = origin.length - 1;
21+
origin[i] = origin[reversePosition];
22+
origin[reversePosition] = temp;
23+
}
24+
}
25+
26+
/**
27+
* 现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5}
28+
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: {1,3,4,5,6,6,5,4,7,6,7,5}
29+
*
30+
* @param oldArray
31+
* @return
32+
*/
33+
34+
public int[] removeZero(int[] oldArray) {
35+
int count = 0;
36+
for (int i : oldArray) {
37+
if (i != 0)
38+
count++;
39+
}
40+
int[] newArray = new int[count];
41+
int currentPos = 0;
42+
for (int i = 0; i < oldArray.length; i++) {
43+
if (oldArray[i] != 0)
44+
newArray[currentPos++] = oldArray[i];
45+
}
46+
return newArray;
47+
}
48+
49+
/**
50+
* 给定两个已经排序好的整形数组, a1和a2 , 创建一个新的数组a3, 使得a3 包含a1和a2 的所有元素, 并且仍然是有序的 例如 a1 =
51+
* [3, 5, 7,8] a2 = [4, 5, 6,7] 则 a3 为[3,4,5,6,7,8] , 注意: 已经消除了重复
52+
*
53+
* @param array1
54+
* @param array2
55+
* @return
56+
*/
57+
58+
public int[] merge(int[] array1, int[] array2) {
59+
int[] result = new int[array1.length + array2.length];
60+
int ptr1 = 0, ptr2 = 0, ptrResult = 0;
61+
while (ptr1 < array1.length && ptr2 < array2.length) {
62+
if (array1[ptr1] == array2[ptr2]) {
63+
result[ptrResult++] = array1[ptr1];
64+
ptr1++;
65+
ptr2++;
66+
continue;
67+
}
68+
if (array1[ptr1] < array2[ptr2]) {
69+
result[ptrResult++] = array1[ptr1];
70+
ptr1++;
71+
continue;
72+
}
73+
else {
74+
result[ptrResult++] = array2[ptr2];
75+
ptr2++;
76+
}
77+
}
78+
if (ptr1 == array1.length) {
79+
for (; ptrResult < result.length; ptrResult++) {
80+
result[ptrResult] = array2[ptr2++];
81+
}
82+
}
83+
else {
84+
for (; ptrResult < result.length; ptrResult++) {
85+
result[ptrResult] = array1[ptr1++];
86+
}
87+
}
88+
return result;
89+
}
90+
91+
/**
92+
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
93+
* 注意,老数组的元素在新数组中需要保持 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
94+
* [2,3,6,0,0,0]
95+
*
96+
* @param oldArray
97+
* @param size
98+
* @return
99+
*/
100+
public int[] grow(int[] oldArray, int size) {
101+
return Arrays.copyOf(oldArray, size);
102+
}
103+
104+
/**
105+
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列 例如, max = 15 ,
106+
* 则返回的数组应该为 [1,1,2,3,5,8,13] max = 1, 则返回空数组 []
107+
*
108+
* @param max
109+
* @return
110+
*/
111+
public int[] fibonacci(int max) {
112+
if (max <= 1)
113+
return new int[0];
114+
List<Integer> fList = new ArrayList<>();
115+
fList.add(1);
116+
fList.add(1);
117+
int last = fList.size() - 1;
118+
while (fList.get(last) < max) {
119+
fList.add(fList.get(last) + fList.get(last - 1));
120+
last++;
121+
}
122+
int[] result = new int[fList.size() - 1];
123+
for (int i = 0; i < result.length; i++) {
124+
result[i] = fList.get(i);
125+
}
126+
return result;
127+
}
128+
129+
/**
130+
* 返回小于给定最大值max的所有素数数组 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
131+
*
132+
* @param max
133+
* @return
134+
*/
135+
public int[] getPrimes(int max) {
136+
boolean[] isPrime = new boolean[max];
137+
List<Integer> primes = new ArrayList<>();
138+
for (int i = 0; i < isPrime.length; i++) {
139+
isPrime[i] = true;
140+
}
141+
for (int i = 2; i * i < max; i++) {
142+
for (int j = i; i * j < max; j++)
143+
isPrime[i * j] = false;
144+
}
145+
for (int i = 2; i < isPrime.length; i++) {
146+
if (isPrime[i])
147+
primes.add(i);
148+
}
149+
int[] result = new int[primes.size()];
150+
for (int i = 0; i < result.length; i++) {
151+
result[i] = primes.get(i);
152+
}
153+
return result;
154+
}
155+
156+
/**
157+
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
158+
*
159+
* @param max
160+
* @return
161+
*/
162+
public int[] getPerfectNumbers(int max) {
163+
int sum = 0;
164+
ArrayList<Integer> perfectNumbers = new ArrayList<>();
165+
for (int i = 1; i < max; i++) {
166+
for (int j = 1; j < i; j++) {
167+
if (i % j == 0) {
168+
sum += j;
169+
}
170+
}
171+
if (sum == i)
172+
perfectNumbers.add(i);
173+
sum = 0;
174+
}
175+
176+
int[] result = new int[perfectNumbers.size()];
177+
for (int i = 0; i < result.length; i++) {
178+
result[i] = perfectNumbers.get(i);
179+
}
180+
return result;
181+
}
182+
183+
/**
184+
* 用seperator 把数组 array给连接起来 例如array= [3,8,9], seperator = "-" 则返回值为"3-8-9"
185+
*
186+
* @param array
187+
* @param s
188+
* @return
189+
*/
190+
public String join(int[] array, String seperator) {
191+
StringBuilder stringBuilder = new StringBuilder();
192+
for (int i : array) {
193+
stringBuilder.append(i + seperator);
194+
}
195+
stringBuilder.delete(stringBuilder.length() - seperator.length(), stringBuilder.length());
196+
197+
return stringBuilder.toString();
198+
}
199+
200+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package assignment2_26;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import org.junit.Test;
7+
8+
public class ArrayUtilTest {
9+
ArrayUtil arrayUtil = new ArrayUtil();
10+
11+
@Test
12+
public void testReverseArray() {
13+
int[] array = new int[] {};
14+
arrayUtil.reverseArray(array);
15+
assertArrayEquals(new int[] {}, array);
16+
17+
array = new int[] { 1 };
18+
arrayUtil.reverseArray(array);
19+
assertArrayEquals(new int[] { 1 }, array);
20+
21+
array = new int[] { 1, 2, 3 };
22+
arrayUtil.reverseArray(array);
23+
assertArrayEquals(new int[] { 3, 2, 1 }, array);
24+
}
25+
26+
@Test
27+
public void testRemoveZero() {
28+
int[] array = new int[] {};
29+
assertArrayEquals(new int[] {}, arrayUtil.removeZero(array));
30+
31+
array = new int[] { 0 };
32+
assertArrayEquals(new int[] {}, arrayUtil.removeZero(array));
33+
34+
array = new int[] { 1 };
35+
assertArrayEquals(new int[] { 1 }, arrayUtil.removeZero(array));
36+
37+
array = new int[] { 1, 2, 0, 0, 3 };
38+
assertArrayEquals(new int[] { 1, 2, 3 }, arrayUtil.removeZero(array));
39+
40+
array = new int[] { 1, 2, 3 };
41+
assertArrayEquals(new int[] { 1, 2, 3 }, arrayUtil.removeZero(array));
42+
}
43+
44+
@Test
45+
public void testMerge() {
46+
int[] array1 = { 3, 5, 7, 8 };
47+
int[] array2 = { 4, 5, 6, 7 };
48+
assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8 }, arrayUtil.merge(array1, array2));
49+
}
50+
51+
@Test
52+
public void testGrow() {
53+
int[] array = { 3, 5, 7 };
54+
assertArrayEquals(new int[] { 3, 5, 7, 0, 0 }, arrayUtil.grow(array, 5));
55+
assertArrayEquals(new int[] { 3, 5, 7 }, arrayUtil.grow(array, 3));
56+
}
57+
58+
@Test
59+
public void testFibonacci() {
60+
assertArrayEquals(new int[] {}, arrayUtil.fibonacci(1));
61+
62+
assertArrayEquals(new int[] { 1, 1 }, arrayUtil.fibonacci(2));
63+
64+
assertArrayEquals(new int[] { 1, 1, 2, 3, 5, 8, 13 }, arrayUtil.fibonacci(15));
65+
}
66+
67+
@Test
68+
public void testGetPrimes() {
69+
assertArrayEquals(new int[] {}, arrayUtil.getPrimes(1));
70+
71+
assertArrayEquals(new int[] {}, arrayUtil.getPrimes(2));
72+
73+
assertArrayEquals(new int[] { 2 }, arrayUtil.getPrimes(3));
74+
75+
assertArrayEquals(new int[] { 2, 3, 5, 7, 11, 13, 17, 19 }, arrayUtil.getPrimes(20));
76+
}
77+
78+
@Test
79+
public void testGetPerfectNumbers() {
80+
assertArrayEquals(new int[] { 6 }, arrayUtil.getPerfectNumbers(10));
81+
82+
assertArrayEquals(new int[] { 6, 28 }, arrayUtil.getPerfectNumbers(100));
83+
84+
assertArrayEquals(new int[] { 6, 28, 496 }, arrayUtil.getPerfectNumbers(1000));
85+
86+
assertArrayEquals(new int[] { 6, 28, 496, 8128 }, arrayUtil.getPerfectNumbers(10000));
87+
}
88+
89+
@Test
90+
public void testJoin() {
91+
assertEquals("3-4-5", arrayUtil.join(new int[] { 3, 4, 5 }, "-"));
92+
93+
assertEquals("345", arrayUtil.join(new int[] { 3, 4, 5 }, ""));
94+
95+
assertEquals("3", arrayUtil.join(new int[] { 3 }, ""));
96+
97+
assertEquals("3--4--5", arrayUtil.join(new int[] { 3, 4, 5 }, "--"));
98+
}
99+
100+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package assignment2_26;
2+
3+
/**
4+
* 这是一个用来展示登录的业务类, 其中的用户名和密码都是硬编码的。
5+
*
6+
* @author liuxin
7+
*
8+
*/
9+
public class LoginAction {
10+
private String name;
11+
private String password;
12+
private String message;
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public String getPassword() {
19+
return password;
20+
}
21+
22+
public String execute() {
23+
if ("test".equals(name) && "1234".equals(password)) {
24+
this.message = "login successful";
25+
return "success";
26+
}
27+
this.message = "login failed,please check your user/pwd";
28+
return "fail";
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public void setPassword(String password) {
36+
this.password = password;
37+
}
38+
39+
public String getMessage() {
40+
return this.message;
41+
}
42+
}

0 commit comments

Comments
 (0)