Skip to content

Commit

Permalink
Merge pull request #32 from Lizhny/master
Browse files Browse the repository at this point in the history
补交第三次作业,多线程下载
  • Loading branch information
BlindingDark authored Mar 31, 2017
2 parents 3cdc5f3 + 9cb7321 commit 4cf00ff
Show file tree
Hide file tree
Showing 15 changed files with 772 additions and 25 deletions.
56 changes: 31 additions & 25 deletions group26/lizhy2017/homework/second/array/ArrayUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class ArrayUtil {
* 例如: a = [7, 9 , 30, 3] , 置换后为 [3, 30, 9,7]
* 如果 a = [7, 9, 30, 3, 4] , 置换后为 [4,3, 30 , 9,7]
*
* @param origin
* @param origin 整形数组
*/
public void reverseArray(int[] origin) {
if (null != origin) return;
for (int i = 0; i < origin.length; i++) {
public static void reverseArray(int[] origin) {
if (null == origin) return;
for (int i = 0; i < origin.length / 2; i++) {
int temp = origin[i];
origin[i] = origin[origin.length - i - 1];
origin[i] = temp;
origin[origin.length - i - 1] = temp;
}
}

Expand All @@ -29,16 +29,18 @@ public void reverseArray(int[] origin) {
* @return
*/

public int[] removeZero(int[] oldArray) {
if (null != oldArray) {
return null;
}
public static int[] removeZero(int[] oldArray) {
if (null == oldArray) return null;
int size = oldArray.length;
for (int i = 0; i < oldArray.length; i++) {
if (oldArray[i] == 0) {
System.arraycopy(oldArray, i + 1, oldArray, i, oldArray.length - i - 1);
size--;
}
}
return oldArray;
int[] newArray = new int[size];
System.arraycopy(oldArray, 0, newArray, 0, size);
return newArray;
}

/**
Expand All @@ -50,30 +52,34 @@ public int[] removeZero(int[] oldArray) {
* @return
*/

public int[] merge(int[] array1, int[] array2) {
if (null != array1 && null != array2) return null;
public static int[] merge(int[] array1, int[] array2) {
if (null == array1 || null == array2) return null;
int[] temp = new int[array1.length + array2.length];
int i = 0, j = 0;
if (array1.length >= array2.length) {
while (i < array2.length) {
i++;
if (array1[i] <= array2[i])
temp[i] = array1[i];
else
temp[i] = array2[i];
i++;
}
System.arraycopy(array1, i + 1, temp, i + 1, temp.length - i - 1);
System.arraycopy(array1, i - 1, temp, 2 * i - 1, temp.length - 2 * i - 1);
} else {
while (j < array1.length) {
j++;
if (array1[j] <= array2[j])
while (j < array1.length - 1) {
if (array1[j] <= array2[j]) {
temp[j] = array1[j];
else
if (array1[j + 1] > array2[j]) {
temp[j] = array2[j];
}
} else
temp[j] = array2[j];
j++;

}
System.arraycopy(array1, j + 1, temp, j + 1, temp.length - j - 1);
System.arraycopy(array2, j - 1, temp, 2 * j - 1, temp.length - 2 * j - 1);
}
return null;
return temp;
}

/**
Expand All @@ -86,7 +92,7 @@ public int[] merge(int[] array1, int[] array2) {
* @param size
* @return
*/
public int[] grow(int[] oldArray, int size) {
public static int[] grow(int[] oldArray, int size) {
int oldCapacity = oldArray.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity < size) {
Expand All @@ -106,7 +112,7 @@ public int[] grow(int[] oldArray, int size) {
* @param max
* @return
*/
public int[] fibonacci(int max) {
public static int[] fibonacci(int max) {
if (max <= 1)
return new int[0];
int[] temp = new int[max];
Expand All @@ -131,7 +137,7 @@ public int[] fibonacci(int max) {
* @param max
* @return
*/
// public int[] getPrimes(int max) {
// public static int[] getPrimes(int max) {
// int[] temp = new int[max];
// if (max < 2)
// return new int[0];
Expand Down Expand Up @@ -165,7 +171,7 @@ public int[] fibonacci(int max) {
* @param max
* @return
*/
public int[] getPerfectNumbers(int max) {
public static int[] getPerfectNumbers(int max) {
int[] temp = new int[max];
int index = 0;
for (int i = 1; i <= max; i++) {
Expand All @@ -192,7 +198,7 @@ public int[] getPerfectNumbers(int max) {
* @param seperator
* @return
*/
public String join(int[] array, String seperator) {
public static String join(int[] array, String seperator) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1) {
Expand Down
90 changes: 90 additions & 0 deletions group26/lizhy2017/homework/second/array/ArrayUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package second.array;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* ${}
* Created by spark_lizhy on 2017/3/20.
*/

public class ArrayUtilTest {

private int[] temp;
private int size;

@Before
public void setUp() throws Exception {
size = 10;
temp = new int[size];
for (int i = 0; i < size; i++) {
temp[i] = i;
}

}

@Test
public void reverseArray() throws Exception {
ArrayUtil.reverseArray(temp);
for (int i = 0; i < size; i++) {
Assert.assertEquals(size - 1 - i, temp[i]);
}
}

@Test
public void removeZero() throws Exception {
for (int i = 0; i < size; i++) {
if (i % 5 == 0) {
temp[i] = 0;
} else {
temp[i] = i;
}
}

temp = ArrayUtil.removeZero(temp);
Assert.assertNotNull(temp);
for (int i = 0; i < temp.length; i++) {
if (temp[i] != 0) {
continue;
}
Assert.assertEquals(temp[i], i);
}

// 测试空数组
{
int[] testArray = new int[5];
for (int i = 0; i < 5; i++) {
testArray[i] = 0;
}

int[] newArray = ArrayUtil.removeZero(testArray);
Assert.assertNotNull(newArray);
Assert.assertEquals(newArray.length, 0);
}
}

@Test
public void merge() throws Exception {
// 构建数组
int[] array1 = new int[10];
int[] array2 = new int[11];
array2[10] = 100;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
array1[i / 2] = i; // 0, 2, 4, 6, 8
} else {
array2[i / 2] = i; // 1, 3, 5, 7, 9
}
}

// 测试merge
{
int[] merge = ArrayUtil.merge(array1, array2);
Assert.assertNotNull(merge);
Assert.assertEquals(merge.length, 21);
}

}

}
32 changes: 32 additions & 0 deletions group26/lizhy2017/homework/third/basic/LRUPageFameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package third.basic;

import org.junit.Assert;
import org.junit.Test;

/**
* ${}
* Created by spark_lizhy on 2017/3/31.
*/

public class LRUPageFameTest {
@Test
public void testAccess() {
LRUPageFrame frame = new LRUPageFrame(3);
frame.access(7);
frame.access(0);
frame.access(1);
Assert.assertEquals("1,0,7", frame.toString());
frame.access(2);
Assert.assertEquals("2,1,0", frame.toString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
frame.access(0);
Assert.assertEquals("0,2,1", frame.toString());
frame.access(3);
Assert.assertEquals("3,0,2", frame.toString());
frame.access(0);
Assert.assertEquals("0,3,2", frame.toString());
frame.access(4);
Assert.assertEquals("4,0,3", frame.toString());
}
}
63 changes: 63 additions & 0 deletions group26/lizhy2017/homework/third/basic/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package third.basic;

/**
* ${}
* Created by spark_lizhy on 2017/3/31.
*/
/**
* 用双向链表实现 LRU 算法
* @author liuxin
*
*/
public class LRUPageFrame {

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}
}

private int capacity;


private Node first;// 链表头
private Node last;// 链表尾


public LRUPageFrame(int capacity) {

this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @return
*/
public void access(int pageNum) {


}



public String toString(){
StringBuilder buffer = new StringBuilder();
Node node = first;
while(node != null){
buffer.append(node.pageNum);

node = node.next;
if(node != null){
buffer.append(",");
}
}
return buffer.toString();
}

}
Loading

0 comments on commit 4cf00ff

Please sign in to comment.