Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions group14/296933284/DataStructuresTest/.classpath

This file was deleted.

14 changes: 0 additions & 14 deletions group14/296933284/DataStructuresTest/.gitignore

This file was deleted.

3 changes: 2 additions & 1 deletion group14/296933284/Note/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 2017编程提高(Java)学习系列笔记链接
---

1. [漫谈计算机组成 -- 微型计算机的硬件组成 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/)
1. [漫谈计算机组成 -- 微型计算机的硬件组成 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/)
2. [Java中解析XML的方法 2017-03-04](http://tennyson.ren/2017/03/04/Java%E4%B8%AD%E8%A7%A3%E6%9E%90XML%E7%9A%84%E6%96%B9%E6%B3%95/#more)
1 change: 0 additions & 1 deletion group14/296933284/Note/test.txt

This file was deleted.

2 changes: 1 addition & 1 deletion group14/296933284/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 2017年编程提高(Java) 作业、练习、总结

[DataStructuresTest --- 基本数据结构Java实现](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/DataStructuresTest)
[coding --- 代码练习](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/coding)
[Note --- 2017编程提高(Java)学习系列笔记链接](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/Note)
10 changes: 10 additions & 0 deletions group14/296933284/coding/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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="lib" path="lib/junit-4.12.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/hamcrest-core-1.3"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/dom4j-1.6.1"/>
<classpathentry kind="var" path="APPLICATION_HOME_DIR/redist/annotations-java8.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 6 additions & 0 deletions group14/296933284/coding/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/bin/
.eml
.iml
.idea
/lib/
*.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding//src/com/coderising/litestruts/StrutsTest.java=UTF-8
228 changes: 228 additions & 0 deletions group14/296933284/coding/src/com/coderising/array/ArrayUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
package com.coderising.array;

import com.coding.basic.ArrayList;

import java.util.Arrays;


/**
* Created by Tennyson on 2017/3/1.
*/
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
*/
public void reverseArray(int[] origin) {
for (int i = 0, j = origin.length - 1; i < origin.length / 2; i++, j--) {
int temp = origin[j];
origin[j] = origin[i];
origin[i] = temp;
}
}

/**
* 现在有如下的一个数组: 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 int[] removeZero(int[] oldArray) {

int i;
// 获取数组中第一个值为 0 的元素的位置
for (i = 0; i < oldArray.length; i++)
if (oldArray[i] == 0)
break;
// 从第一个值为 0 的元素开始,用之后的非零元素覆盖之前值为 0 的元素,并记录非零元素的个数
for (int j = i + 1; j < oldArray.length; j++)
if (oldArray[j] != 0)
oldArray[i++] = oldArray[j];

// 复制数组
int[] newArray = new int[i];
System.arraycopy(oldArray, 0, newArray, 0, i);

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 int[] merge(int[] array1, int[] array2) {
int i = 0, j = 0;
ArrayList arrayList = new ArrayList();

// 比较两个数组的元素值,将较小的值存入arrayList
while (i < array1.length && j < array2.length) {

if (array1[i] == array2[j]) {
arrayList.add(array1[i]);
i++;
j++;
} else if (array1[i] < array2[j]) {
arrayList.add(array1[i++]);
} else if (array1[i] > array2[j]) {
arrayList.add(array2[j++]);
}
}

// 剩下一个数组还未比较完, 将其剩余元素存入arrayList
while (i < array1.length) arrayList.add(array1[i++]);
while (j < array2.length) arrayList.add(array2[j++]);

return ArrayUtil.toArray(arrayList);
}
/**
* 把一个已经存满数据的数组 oldArray的容量进行扩展, 扩展后的新数据大小为oldArray.length + size
* 注意,老数组的元素在新数组中需要保持
* 例如 oldArray = [2,3,6] , size = 3,则返回的新数组为
* [2,3,6,0,0,0]
* @param oldArray
* @param size
* @return
*/
public int[] grow(int [] oldArray, int size) {
int[] newArray = new int[oldArray.length + size];

return newArray = Arrays.copyOf(oldArray, newArray.length);
}

/**
* 斐波那契数列为:1,1,2,3,5,8,13,21...... ,给定一个最大值, 返回小于该值的数列
* 例如, max = 15 , 则返回的数组应该为 [1,1,2,3,5,8,13]
* max = 1, 则返回空数组 []
* @param max
* @return
*/
public int[] fibonacci(int max) {

ArrayList arrayList = new ArrayList();
arrayList.add(1);
arrayList.add(1);

int i, num;
for (i = 2; (num = (int) (arrayList.get(i - 1)) + (int) (arrayList.get(i - 2))) < max; i++)
arrayList.add(num);

return ArrayUtil.toArray(arrayList);
}

/**
* 返回小于给定最大值max的所有素数数组
* 例如max = 23, 返回的数组为[2,3,5,7,11,13,17,19]
* @param max
* @return
*/
public int[] getPrimes(int max) {

ArrayList arrayList = new ArrayList();

for (int i = 2; i < max; i++) {
int k = (int) Math.sqrt(i), j;
for (j = 2; j <= k; j++)
if (i % j == 0) break;

// 若是 j >= k + 1 表示 i 没有被整除 即 i 为素数
if (j >= k + 1)
arrayList.add(i);
}

return ArrayUtil.toArray(arrayList);
}

/**
* 所谓“完数”, 是指这个数恰好等于它的因子之和,例如6=1+2+3
* 给定一个最大值max, 返回一个数组, 数组中是小于max 的所有完数
* @param max
* @return
*/
public int[] getPerfectNumbers(int max) {

ArrayList arrayList = new ArrayList();

for (int i = 6; i < max; i += 2) {

if (i == 6) {
arrayList.add(i);
continue;
}

int factorSum = 0;

if (i % 3 == 1 && i % 9 == 1) {

boolean flag = false;

if (i % 10 == 6 ) {
flag = true;
}

if (i % 10 == 8) {
if (i % 100 == 28) {
flag = true;
}
}

if (flag) {

for (int j = 1; j <= (i / 2); j++) {

if (i % j == 0) {
factorSum += j;
}
}
}

}

if (factorSum == i) {
arrayList.add(i);
}

}

return ArrayUtil.toArray(arrayList);
}

// 将ArrayList对象转换为数组返回
private static int[] toArray(ArrayList arrayList) {
int[] array = new int[arrayList.size()];

for (int i = 0; i < array.length; i++)
array[i] = (Integer) arrayList.get(i);

return array;
}

/**
* 用seperator 把数组 array给连接起来
* 例如array= [3,8,9], seperator = "-"
* 则返回值为"3-8-9"
* @param array
* @param seperator
* @return
*/
public String join(int[] array, String seperator){

StringBuffer stringBuffer = new StringBuffer();

for (int i = 0; i < array.length - 1; i++)
stringBuffer.append("" + array[i] + seperator);

stringBuffer.append(array[array.length - 1]);

return stringBuffer.toString();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.coderising.array;

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


/**
* Created by damocles on 2017/3/1.
*/
public class ArrayUtilTest {
private ArrayUtil arrayUtil;
private int[] oldArray = null;
private int[] newArray = null;

@Before
public void setUp() throws Exception {
arrayUtil = new ArrayUtil();
}

@After
public void tearDown() throws Exception {
}


@Test
public void reverseArray() throws Exception {
oldArray = new int[]{7, 9, 30, 3};
newArray = new int[]{3, 30, 9, 7};
arrayUtil.reverseArray(oldArray);

Assert.assertArrayEquals(newArray, oldArray);

oldArray = new int[]{3, 30, 9, 7, 4};
newArray = new int[]{4, 7, 9, 30, 3};
arrayUtil.reverseArray(oldArray);

Assert.assertArrayEquals(newArray, oldArray);
}

@Test
public void removeZero() throws Exception {
oldArray = new int[]{1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
newArray = new int[]{1, 3, 4, 5, 6, 6, 5, 4, 7, 6, 7, 5};

Assert.assertArrayEquals(newArray, arrayUtil.removeZero(oldArray));

}

@Test
public void merge() throws Exception {
int[] a1 = {3, 5, 7, 8};
int[] a2 = {4, 5, 6, 7};
newArray = new int[]{3, 4, 5, 6, 7, 8};

Assert.assertArrayEquals(newArray, arrayUtil.merge(a1, a2));
}

@Test
public void grow() throws Exception {
oldArray = new int[]{2, 3, 6};
newArray = new int[]{2, 3, 6, 0, 0, 0};
int size = 3;

Assert.assertArrayEquals(newArray, arrayUtil.grow(oldArray, size));
}

@Test
public void fibonacci() throws Exception {
newArray = new int[]{1, 1, 2, 3, 5, 8, 13};

Assert.assertArrayEquals(newArray, arrayUtil.fibonacci(15));
}

@Test
public void getPrimes() throws Exception {
newArray = new int[]{2, 3, 5, 7, 11, 13, 17, 19};

Assert.assertArrayEquals(newArray, arrayUtil.getPrimes(23));
}

@Test
public void getPerfectNumbers() throws Exception {
newArray = new int[]{6, 28, 496, 8128};
System.out.println(33550336 % 27);
Assert.assertArrayEquals(newArray, arrayUtil.getPerfectNumbers(10000));
}

@Test
public void join() throws Exception {
oldArray = new int[]{3, 8, 9, 10, 2};
String seperator = "-";
String exception = "3-8-9-10-2";

Assert.assertEquals(exception, arrayUtil.join(oldArray, seperator));
}

}
Loading