Skip to content

Commit 55a2be6

Browse files
authored
Merge pull request onlyliuxin#1 from luoziyihao/master
update
2 parents 627fdda + 32c4abc commit 55a2be6

File tree

15 files changed

+484
-1
lines changed

15 files changed

+484
-1
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
*.class
2-
32
# Mobile Tools for Java (J2ME)
43
.mtj.tmp/
54

@@ -14,3 +13,9 @@ hs_err_pid*
1413
#ide config
1514
.metadata
1615
.recommenders
16+
.idea/
17+
*.iml
18+
rebel.*
19+
.rebel.*
20+
21+
target
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>basic</artifactId>
5+
<parent>
6+
<groupId>com.coding</groupId>
7+
<artifactId>parent</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../parent/pom.xml</relativePath>
10+
</parent>
11+
12+
</project>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayList implements List {
6+
7+
private int size = 0;
8+
9+
private Object[] elementData = new Object[100];
10+
11+
private int length() {
12+
return elementData.length;
13+
}
14+
15+
private static final int ENLARGE_LENGTH = 100;
16+
17+
private Object[] enlarge(Object[] origin) {
18+
return Arrays.copyOf(origin, origin.length + ENLARGE_LENGTH);
19+
}
20+
21+
private void enLargeElementData() {
22+
if (size == length()) {
23+
elementData = enlarge(elementData);
24+
}
25+
}
26+
27+
public void add(Object o) {
28+
enLargeElementData();
29+
elementData[size] = o;
30+
size++;
31+
}
32+
33+
public void add(int index, Object o) {
34+
checkIndex(index);
35+
enLargeElementData();
36+
// 备份 index 处及后面的数据
37+
Object[] elementsBehindIndex = backBehindElements(elementData, index);
38+
// 给index处 设值
39+
elementData[index] = o;
40+
// 追加 备份的数据
41+
appendElement(elementData, index, elementsBehindIndex);
42+
size++;
43+
}
44+
45+
private void appendElement(Object[] origin, int pos, Object[] append) {
46+
System.arraycopy(append, 0, origin, pos, append.length);
47+
}
48+
49+
private Object[] backBehindElements(Object[] elementData, int index) {
50+
int backSize = size - index;
51+
Object[] back = new Object[backSize];
52+
System.arraycopy(elementData, index, back, 0, backSize);
53+
return back;
54+
}
55+
56+
public Object get(int index) {
57+
checkIndex(index);
58+
return elementData[index];
59+
}
60+
61+
private void checkIndex(int index) {
62+
if (index < 0 || index >= size) {
63+
throw new ArrayIndexOutOfBoundsException(String.format("index=%s, size=%s", index, size));
64+
}
65+
}
66+
67+
public Object remove(int index) {
68+
checkIndex(index);
69+
Object[] back = backBehindElements(elementData, index + 1);
70+
System.arraycopy(back, 0, elementData, index, back.length);
71+
Object ret = elementData[index];
72+
elementData[index] = null;
73+
size--;
74+
return ret;
75+
}
76+
77+
public int size() {
78+
return size;
79+
}
80+
81+
public Iterator iterator() {
82+
return null;
83+
}
84+
85+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coding.basic;
2+
3+
public class BinaryTreeNode {
4+
5+
private Object data;
6+
private BinaryTreeNode left;
7+
private BinaryTreeNode right;
8+
9+
public Object getData() {
10+
return data;
11+
}
12+
public void setData(Object data) {
13+
this.data = data;
14+
}
15+
public BinaryTreeNode getLeft() {
16+
return left;
17+
}
18+
public void setLeft(BinaryTreeNode left) {
19+
this.left = left;
20+
}
21+
public BinaryTreeNode getRight() {
22+
return right;
23+
}
24+
public void setRight(BinaryTreeNode right) {
25+
this.right = right;
26+
}
27+
28+
public BinaryTreeNode insert(Object o){
29+
return null;
30+
}
31+
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.coding.basic;
2+
3+
public class LinkedList implements List {
4+
5+
private Node head;
6+
7+
public void add(Object o){
8+
9+
}
10+
public void add(int index , Object o){
11+
12+
}
13+
public Object get(int index){
14+
return null;
15+
}
16+
public Object remove(int index){
17+
return null;
18+
}
19+
20+
public int size(){
21+
return -1;
22+
}
23+
24+
public void addFirst(Object o){
25+
26+
}
27+
public void addLast(Object o){
28+
29+
}
30+
public Object removeFirst(){
31+
return null;
32+
}
33+
public Object removeLast(){
34+
return null;
35+
}
36+
public Iterator iterator(){
37+
return null;
38+
}
39+
40+
41+
private static class Node{
42+
Object data;
43+
Node next;
44+
45+
}
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coding.basic;
2+
3+
public interface List {
4+
public void add(Object o);
5+
public void add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.coding.basic;
2+
3+
public class Queue {
4+
5+
public void enQueue(Object o){
6+
}
7+
8+
public Object deQueue(){
9+
return null;
10+
}
11+
12+
public boolean isEmpty(){
13+
return false;
14+
}
15+
16+
public int size(){
17+
return -1;
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coding.basic;
2+
3+
public class Stack {
4+
private ArrayList elementData = new ArrayList();
5+
6+
public void push(Object o){
7+
}
8+
9+
public Object pop(){
10+
return null;
11+
}
12+
13+
public Object peek(){
14+
return null;
15+
}
16+
public boolean isEmpty(){
17+
return false;
18+
}
19+
public int size(){
20+
return -1;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coding.api;
2+
3+
import org.junit.Test;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Created by luoziyihao on 2/25/17.
11+
*/
12+
public class ArraysTest {
13+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
14+
15+
16+
@Test
17+
public void testCopyOf(){
18+
Object[] a = new Object[]{1, 2, 3, 4};
19+
Object[] b = Arrays.copyOf(a, 10);
20+
logger.info("a={}, b={}", Arrays.toString(a), Arrays.toString(b));
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.coding.api;
2+
3+
import org.junit.Test;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.Arrays;
8+
9+
/**
10+
* Created by luoziyihao on 2/25/17.
11+
*/
12+
public class SystemTest {
13+
14+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
15+
16+
@Test
17+
public void testArrayCopy() {
18+
int[] a = new int[]{1, 2, 3, 4, 5, 6, 7};
19+
int[] b = new int[]{11, 22, 33, 44, 55, 66, 77};
20+
System.arraycopy(a, 2, b, 4, 3);
21+
logger.info("b={}", Arrays.toString(b));
22+
23+
}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.coding.basic;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
9+
/**
10+
* Created by luoziyihao on 2/25/17.
11+
*/
12+
public class ArrayListTest {
13+
14+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
15+
16+
17+
private List list = new ArrayList();
18+
19+
@Before
20+
public void before() {
21+
22+
}
23+
24+
@Test
25+
public void add() throws Exception {
26+
list.add(1);
27+
}
28+
29+
@Test
30+
public void get() throws Exception {
31+
add();
32+
logger.info("{}", list.get(0));
33+
}
34+
35+
}

0 commit comments

Comments
 (0)