Skip to content

Commit ce769fd

Browse files
author
songbao.yang
committed
task1
1 parent 2466967 commit ce769fd

File tree

14 files changed

+736
-0
lines changed

14 files changed

+736
-0
lines changed

group05/578505552/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.class
2+
3+
# Mobile Tools for Java (J2ME)
4+
.mtj.tmp/
5+
6+
# Package Files #
7+
*.jar
8+
*.war
9+
*.ear
10+
11+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
12+
hs_err_pid*
13+
14+
#ide config
15+
.metadata
16+
.recommenders
17+
.idea/

group05/578505552/578505552.iml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
9+
<excludeFolder url="file://$MODULE_DIR$/target" />
10+
</content>
11+
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
14+
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
15+
</component>
16+
</module>

group05/578505552/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
5+
<groupId>com.coding2017.yang</groupId>
6+
<artifactId>basic</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>basic</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.12</version>
22+
</dependency>
23+
</dependencies>
24+
</project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.coding.basic;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Created by songbao.yang on 2017/2/21.
7+
*
8+
*/
9+
public class ArrayList implements List {
10+
11+
private int size = 0;
12+
private Object[] elementData;
13+
private static final int MIN_CAPACITY = 10;
14+
15+
public ArrayList(int size) {
16+
if (size < 0){
17+
throw new IllegalArgumentException("illega size: " + size);
18+
}
19+
this.elementData = new Object[size];
20+
}
21+
22+
public ArrayList() {
23+
this.elementData = new Object[0];
24+
}
25+
26+
public void add(Object o){
27+
ensureCapacity(size + 1);
28+
elementData[size++] = o;
29+
}
30+
31+
private void ensureCapacity(int minCapacity){
32+
if (minCapacity < 0 ){
33+
throw new OutOfMemoryError();
34+
}
35+
36+
int newCapcity = size;
37+
if(minCapacity < MIN_CAPACITY){
38+
newCapcity = MIN_CAPACITY;
39+
} else if(minCapacity > elementData.length){
40+
int tmp = elementData.length << 1;
41+
newCapcity = tmp > elementData.length ? tmp : Integer.MAX_VALUE;
42+
}
43+
44+
newCapcity = minCapacity;
45+
Object[] newData = new Object[newCapcity];
46+
System.arraycopy(elementData, 0, newData, 0, size);
47+
elementData = newData;
48+
}
49+
50+
public void add(int index, Object o){
51+
indexCheck(index);
52+
ensureCapacity(size+1);
53+
System.arraycopy(elementData, index, elementData, index+1, size-index);
54+
elementData[index] = o;
55+
size++;
56+
}
57+
58+
public Object get(int index){
59+
indexCheck(index);
60+
return elementData[index];
61+
}
62+
63+
private void indexCheck(int index){
64+
if(index < 0){
65+
throw new IllegalArgumentException("illegal index: " + index);
66+
}
67+
if(index >= size){
68+
throw new IndexOutOfBoundsException();
69+
}
70+
}
71+
72+
public Object remove(int index){
73+
indexCheck(index);
74+
Object rm = elementData[index];
75+
System.arraycopy(elementData, index+1, elementData, index, size-index-1);
76+
size--;
77+
return rm;
78+
}
79+
80+
public int size(){
81+
return size;
82+
}
83+
84+
public Iterator iterator(){
85+
return new Itr();
86+
}
87+
88+
//静态内部类的访问权限不同有何区别??
89+
private class Itr implements Iterator{
90+
private int cursor = 0;
91+
92+
public boolean hasNext() {
93+
return cursor != size;
94+
}
95+
96+
public Object next() {
97+
if (hasNext()){
98+
return elementData[cursor++];
99+
}
100+
101+
throw new NoSuchElementException();
102+
}
103+
}
104+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* Created by songbao.yang on 2017/2/21.
5+
*
6+
*/
7+
public class BinaryTreeNode {
8+
9+
private Object data;
10+
private BinaryTreeNode left;
11+
private BinaryTreeNode right;
12+
13+
public Object getData() {
14+
return data;
15+
}
16+
public void setData(Object data) {
17+
this.data = data;
18+
}
19+
public BinaryTreeNode getLeft() {
20+
return left;
21+
}
22+
public void setLeft(BinaryTreeNode left) {
23+
this.left = left;
24+
}
25+
public BinaryTreeNode getRight() {
26+
return right;
27+
}
28+
public void setRight(BinaryTreeNode right) {
29+
this.right = right;
30+
}
31+
32+
public BinaryTreeNode insert(Object o){
33+
return null;
34+
}
35+
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* Created by songbao.yang on 2017/2/21.
5+
*
6+
*/
7+
public interface Iterator {
8+
public boolean hasNext();
9+
public Object next();
10+
11+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* Created by songbao.yang on 2017/2/21.
5+
*
6+
*/
7+
public class LinkedList implements List {
8+
9+
private Node head;
10+
private int elementCount;
11+
12+
//head作为一个节点,其next的值指向List中的真正的第一个节点
13+
public LinkedList() {
14+
Node head = new Node();
15+
head.next = null;
16+
head.data = null;
17+
elementCount = 0;
18+
}
19+
20+
public void add(Object o){
21+
Node newNode = new Node();
22+
newNode.data = o;
23+
newNode.next = null;
24+
25+
Node cursor = head;
26+
while (cursor.next != null){
27+
cursor = cursor.next;
28+
}
29+
cursor.next = newNode;
30+
elementCount++;
31+
}
32+
33+
34+
public void add(int index , Object o){
35+
indexRangeCheck(index);
36+
Node newNode = new Node();
37+
newNode.data = o;
38+
39+
Node cursor = head;
40+
for (int i = 0; i < index; i++) {
41+
cursor = cursor.next; //将cursor移动到index-1节点处;
42+
}
43+
44+
newNode.next = cursor.next; //将新节点指向原index处的节点
45+
cursor.next = newNode;//将原index-1处的节点指向新节点
46+
elementCount++;
47+
}
48+
49+
private void indexRangeCheck(int index){
50+
if (index < 0 || index >= size()){
51+
throw new IndexOutOfBoundsException();
52+
}
53+
}
54+
55+
public Object get(int index){
56+
indexRangeCheck(index);
57+
Node cursor = head;
58+
for (int i = 0; i < index; i++) {
59+
cursor = cursor.next;
60+
}
61+
return cursor.next;
62+
}
63+
64+
public Object remove(int index){
65+
indexRangeCheck(index);
66+
Node cursor = head;
67+
for (int i = 0; i < index; i++) {
68+
cursor = cursor.next;
69+
}
70+
Node indexNode = cursor.next;
71+
cursor.next = indexNode.next;
72+
return indexNode;
73+
}
74+
75+
public int size(){
76+
return elementCount;
77+
}
78+
79+
public void addFirst(Object o){
80+
81+
}
82+
public void addLast(Object o){
83+
84+
}
85+
public Object removeFirst(){
86+
return null;
87+
}
88+
public Object removeLast(){
89+
return null;
90+
}
91+
public Iterator iterator(){
92+
return null;
93+
}
94+
95+
96+
private static class Node{
97+
Object data;
98+
Node next;
99+
}
100+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* Created by songbao.yang on 2017/2/21.
5+
*
6+
*/
7+
public interface List {
8+
public void add(Object o);
9+
public void add(int index, Object o);
10+
public Object get(int index);
11+
public Object remove(int index);
12+
public int size();
13+
}

0 commit comments

Comments
 (0)