Skip to content

Commit d72cd0a

Browse files
authored
Merge pull request #2 from nusubmarine01/master
merge
2 parents 8c6675c + c779489 commit d72cd0a

File tree

112 files changed

+8025
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

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

0 commit comments

Comments
 (0)