Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master' into haolip…
Browse files Browse the repository at this point in the history
…eng/master
  • Loading branch information
Hipple committed Mar 5, 2017
2 parents 27c2158 + 98f0c0c commit ec776c2
Show file tree
Hide file tree
Showing 10 changed files with 542 additions and 0 deletions.
9 changes: 9 additions & 0 deletions group18/1159828430/20170226/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="lib/jaxen-1.1-beta-6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group18/1159828430/20170226/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group18/1159828430/20170226/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>20170226</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
232 changes: 232 additions & 0 deletions group18/1159828430/20170226/src/com/coding/array/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package com.coding.array;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* @author Scholar
* @Time:2017年2月27日 下午8:46:07
* @version 1.0
*/
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
* @return
*/
public static int[] reverseArray(int[] origin){
for (int i = 0; i < origin.length/2; i++) {
int temp = origin[i];
origin[i] = origin[origin.length-i-1];
origin[origin.length-i-1] = temp;
}
return origin;
}

/**
* 现在有如下的一个数组: 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 static int[] removeZero(int[] oldArray){
//正常方式
/*List<Integer> list = new ArrayList<Integer>();
for (int i : oldArray)
if (i != 0)
list.add(i);
int[] newArray = new int[list.size()];
for (int i = 0; i < newArray.length; i++) {
newArray[i] = list.get(i);
}*/
//jdk1.8
IntStream intStream = Arrays.stream(oldArray);
int[] newArray = intStream.filter(i -> i != 0).toArray();
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 static int[] merge(int[] array1, int[] array2){
int[] newArray= new int[array1.length + array2.length];
System.arraycopy(array1, 0, newArray, 0, array1.length);
System.arraycopy(array2, 0, newArray, array1.length, array2.length);

//Arrays.sort(newArray);//快速排序

//冒泡排序
/*for(int i=0;i<newArray.length-1;i++){
for(int j= i + 1; j < newArray.length; j++){
if (newArray[i] > newArray[j]){
int temp = newArray[i];
newArray[i] = newArray[j];
newArray[j] = temp;
}
}
}*/

//选择排序
/*for (int i = 0; i < newArray.length - 1; i++) {
int min = i;
for (int j = i + 1; j < newArray.length; j++) {
if (newArray[min] > newArray[j]) {
min = j;
}
}
if (min != i) {
int temp = newArray[i];
newArray[i] = newArray[min];
newArray[min] = temp;
}
}*/

//插入排序
for (int i = 1; i < newArray.length; i++) {
for (int j = i; j > 0; j--) {
if (newArray[j] < newArray[j - 1]) {
int temp = newArray[j - 1];
newArray[j - 1] = newArray[j];
newArray[j] = temp;
} else
break;
}
}
return newArray;
}
/**
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
* 注意,老数组的元素在新数组中需要保持
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
* [2,3,6,0,0,0]
* @param oldArray
* @param size
* @return
*/
public static int[] grow(int [] oldArray, int size){
oldArray = Arrays.copyOf(oldArray, oldArray.length + size);
return oldArray;
}

/**
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13]
* max = 1, 则返回空数组 []
* @param max
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static int[] fibonacci(int max){
List<Integer> list = new ArrayList<Integer>();
if (max == 1) {
int[] newArr = null;
return newArr;
} else {
for (int i = 0; i <= max; i++) {
int value = fibonacciSequence(i);
if (value < max) {
list.add(value);
} else {
break;
}
}
}
int[] newArray = list.stream().mapToInt(i -> i).toArray();
return newArray;
}

private static int fibonacciSequence(int n){
if(n < 2){
return 1;
}else{
return fibonacciSequence(n-1) + fibonacciSequence(n-2);
}
}
/**
* 返回小于给定最大值max的所有素数数组
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
* @param max
* @return
*/
@SuppressWarnings("unused")
public static int[] getPrimes(int max){
List<Integer> list = new ArrayList<Integer>();
for (int i = 2; i < max; i++) {
list.add(i);
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0){
Integer temp = i;
list.remove(temp);
break;
}
}
}

int[] newArray = list.stream().mapToInt(i -> i).toArray();
return newArray;
}

/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
* @param max
* @return
*/
public static int[] getPerfectNumbers(int max){
List<Integer> list = new ArrayList<Integer>();
int[] newArray = null;
if (!(max < 6)) {//第一个完数为6
for (int i = 6; i < max; i++) {
int sum = 0;
for (int j = 1; j <= i/2; j++) {
if (i % j == 0) {
sum += j;
}
}
if (sum == i) {
list.add(i);
}
System.out.println(i);
}
newArray = list.stream().mapToInt(i -> i).toArray();
}
return newArray;
}

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

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.coding.array;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import org.junit.Test;

/**
* @author Scholar
* @Time:2017年3月4日 上午9:12:27
* @version 1.0
*/
public class ArrayUtilTest {

@Test
public void testReverseArray(){
int[] oldArr = {7, 9 , 30, 3};
int[] newArr = ArrayUtil.reverseArray(oldArr);
int[] resultArr = {3, 30 , 9, 7};
assertArrayEquals(resultArr, newArr);

}

@Test
public void testRemoveZero() {
int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
int[] newArr = ArrayUtil.removeZero(oldArr);
int[] resultArr = {1,3,4,5,6,6,5,4,7,6,7,5};
assertArrayEquals(resultArr, newArr);
}

@Test
public void testMerge() {
int[] a1 = {3,5,7,8};
int[] a2 = {4, 5, 6,7};
int[] newArr = ArrayUtil.merge(a1, a2);
Arrays.stream(newArr).forEach(i -> System.out.println(i));
}

@Test
public void testGrow() {
int[] oldArr = {2,3,6};
int[] newArr = ArrayUtil.grow(oldArr, 3);
int[] resultArr = {2,3,6,0,0,0};
assertArrayEquals(resultArr, newArr);
}

@Test
public void testFibonacci() {
int[] newArr = ArrayUtil.fibonacci(15);
int[] resultArr = {1, 1, 2, 3, 5, 8, 13};
assertArrayEquals(resultArr, newArr);
}

@Test
public void testGetPrimes() {
int[] newArr = ArrayUtil.getPrimes(23);
int[] resultArr = {2,3,5,7,11,13,17,19};
assertArrayEquals(resultArr, newArr);
}

@Test
public void testGetPerfectNumbers() {
int[] newArr = ArrayUtil.getPerfectNumbers(500);
int[] resultArr = {6, 28, 496};
assertArrayEquals(resultArr, newArr);
}

@Test
public void testJoin() {
int[] oldArr = {3,8,9};
String resultStr = ArrayUtil.join(oldArr, "-");
String str = "3-8-9";
assertEquals(str, resultStr);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.coding.litestruts;
/**
* @author Scholar
* @Time:2017年2月27日 下午8:50:23
* @version 1.0
*/
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;
}
}
Loading

0 comments on commit ec776c2

Please sign in to comment.