Skip to content

Commit 76187af

Browse files
authored
Merge pull request onlyliuxin#12 from superfish17/master
第一次作业
2 parents 733e95e + 2169d9f commit 76187af

11 files changed

+455
-0
lines changed

group14/352504906/test/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

group14/352504906/test/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>test</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sun Feb 26 15:56:36 CST 2017
2+
eclipse.preferences.version=1
3+
encoding//src/com/coding/basic=UTF-8
4+
encoding//src/com/coding/basic/SimpleArrayList.java=UTF-8
5+
encoding/<project>=UTF-8
6+
encoding/src=UTF-8
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Sun Feb 26 15:31:53 CST 2017
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
6+
/**
7+
* @Description 简单实现ArrayList
8+
*/
9+
public class SimpleArrayList implements SimpleList{
10+
private int size = 0;
11+
private Object[] elementData = new Object[10];
12+
/**
13+
* 插入元素o
14+
* @param o 待插入元素
15+
*/
16+
public void add(Object o){
17+
//扩容
18+
if(elementData.length < size + 1){
19+
grow(size+1);
20+
}
21+
elementData[size++] = o;
22+
}
23+
/**
24+
* 数组扩容
25+
* @param capacity 数组实际长度
26+
*/
27+
private void grow(int capacity) {
28+
int oldCapacity = elementData.length;
29+
int newCapacity = oldCapacity + (oldCapacity >> 1);//扩容50%
30+
if(capacity > newCapacity){
31+
newCapacity = capacity;
32+
}
33+
elementData = Arrays.copyOf(elementData, newCapacity);
34+
}
35+
/**
36+
* 在指定索引初插入元素
37+
* @param index 索引
38+
* @param o 待插入元素
39+
*/
40+
public void add(int index,Object o){
41+
rangeCheckForAdd(index);
42+
if(elementData.length<size+1){
43+
//扩容
44+
grow(size+1);
45+
}
46+
elementData[index] = o;
47+
size++;
48+
}
49+
/**
50+
* 插入位置越界处理
51+
* @param index 索引
52+
*/
53+
private void rangeCheckForAdd(int index) {
54+
if(index > size || index <0){
55+
throw new IndexOutOfBoundsException("数组越界异常");
56+
}
57+
}
58+
/**
59+
* 索引越界处理
60+
* @param index 索引
61+
*/
62+
private void rangeCheck(int index) {
63+
if(index >= size || index <0)
64+
throw new IndexOutOfBoundsException("数组越界异常");
65+
}
66+
/**
67+
* 移除该索引处元素
68+
* @param index 索引位置
69+
* @return 移除元素
70+
*/
71+
public Object remove(int index){
72+
rangeCheck(index);
73+
Object oldObject = elementData[index];
74+
if(size > index +1){
75+
System.arraycopy(elementData, index +1 , elementData, index, size-index-1);
76+
}
77+
elementData[--size] = null;
78+
return oldObject;
79+
}
80+
/**
81+
* 返回集合长度
82+
* @return 长度
83+
*/
84+
public int size(){
85+
return this.size;
86+
}
87+
/**
88+
* 返回指定索引元素
89+
* @param index 索引
90+
* @return Object 元素
91+
*/
92+
public Object get(int index){
93+
rangeCheck(index);
94+
return elementData[index];
95+
}
96+
private class ArrayListIterator implements SimpleIterator{
97+
SimpleArrayList l = null;
98+
private int iteratorIndex = 0;
99+
100+
private ArrayListIterator(SimpleArrayList arrayList){
101+
this.l = arrayList;
102+
}
103+
104+
@Override
105+
public boolean hasNext() {
106+
return iteratorIndex < size;
107+
}
108+
109+
@Override
110+
public Object next() {
111+
if (iteratorIndex >= size) {
112+
throw new RuntimeException("数组越界异常");
113+
}
114+
return elementData[iteratorIndex++];
115+
}
116+
117+
}
118+
}
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+
*简单迭代器接口
5+
*
6+
*/
7+
public interface SimpleIterator {
8+
public Object next();
9+
public boolean hasNext();
10+
11+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.coding.basic;
2+
/**
3+
* @Description 简单实现linkedList
4+
*/
5+
public class SimpleLinkedList implements SimpleList{
6+
private Node head;
7+
8+
private int size = 0;
9+
/**
10+
* 返回集合长度
11+
* @return 长度
12+
*/
13+
public int size(){
14+
return this.size;
15+
}
16+
/**
17+
* 返回指定索引出元素
18+
* @param index 索引
19+
* @return Object 元素
20+
*/
21+
public Object get(int index){
22+
rangeCheck(index);
23+
Node current = head;
24+
for(int i =0;i<index;i++){
25+
current = current.next;
26+
}
27+
return current.o;
28+
}
29+
/**
30+
* 插入元素o,默认是往头部插入
31+
* @param o 待插入元素
32+
*/
33+
public void add(Object o){
34+
addFirst(o);
35+
}
36+
/**
37+
* 在指定索引处插入元素
38+
* @param index 索引
39+
* @param o 待插入元素
40+
*/
41+
public void add(int index,Object o){
42+
rangeCheck(index);
43+
if(index == 0){
44+
addFirst(0);
45+
return;
46+
}
47+
if(index == size){
48+
addLast(o);
49+
return;
50+
}
51+
Node newNode = new Node(o);
52+
Node current = head;
53+
for(int i = 0;i < index -1;i++){
54+
current = current.next;
55+
}
56+
newNode.next = current.next;
57+
current.next = newNode;
58+
size ++;
59+
60+
}
61+
/**
62+
* 索引越界处理
63+
* @param index 索引
64+
*/
65+
private void rangeCheck(int index) {
66+
if(index > size || index < 0){
67+
throw new RuntimeException("找不到该节点");
68+
}
69+
}
70+
//头插法
71+
public void addFirst(Object o){
72+
Node newNode = new Node(o);
73+
newNode.next = head;
74+
head = newNode;
75+
size++;
76+
}
77+
//尾插法
78+
public void addLast(Object o){
79+
Node newNode = new Node(o);
80+
Node current = head;//设定一个当前节点,便于遍历
81+
while(current.next!=null){
82+
current = current.next;
83+
}
84+
if(current.next == null)//尾节点
85+
{
86+
current.next = newNode;
87+
size++;
88+
}
89+
}
90+
/**
91+
* 移除该索引处元素
92+
* @param index 索引位置
93+
* @return 移除元素
94+
*/
95+
public Object remove(int index){
96+
rangeCheck(index+1);
97+
Node current = head;
98+
if(index == 0){//头部删除
99+
Object removeObj = head.o;
100+
head = head.next;
101+
size --;
102+
return removeObj;
103+
}
104+
int i;
105+
for(i=0;i<index-1;i++){
106+
current = current.next;
107+
}
108+
Object removeObject = get(i);
109+
if(index == size-1){//尾部删除
110+
current.next = null;
111+
size--;
112+
return removeObject;
113+
}
114+
current.next = current.next.next;
115+
size--;
116+
return removeObject;
117+
}
118+
//内部类,声明为static 与实例无关
119+
private static class Node{
120+
private Object o;//单链表的数据域
121+
private Node next;//单链表的指针域
122+
private Node(Object o){
123+
this.o = o;
124+
}
125+
}
126+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* 简单list接口
5+
*
6+
*/
7+
public interface SimpleList {
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+
14+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @Description 简单实现队列
7+
*/
8+
public class SimpleQueue {
9+
private Object[] elementData = new Object[10];
10+
private int size;
11+
12+
/**
13+
* 往队尾添加新的元素
14+
* @param o 要添加的新元素
15+
*/
16+
public void enQueue(Object o){
17+
if(elementData.length < size +1){
18+
//扩容
19+
grow(size+1);
20+
}
21+
elementData[size++] = o;
22+
}
23+
/**
24+
* 数组扩容
25+
* @param capacity 数组实际长度
26+
*/
27+
private void grow(int capacity) {
28+
int oldCapacity = elementData.length;
29+
int newCapacity = oldCapacity * 2;
30+
if(capacity > newCapacity){
31+
newCapacity = capacity;
32+
}
33+
elementData = Arrays.copyOf(elementData, newCapacity);
34+
}
35+
/**
36+
* 移除并返回队列头部元素
37+
* @return 队列头部元素
38+
*/
39+
public Object deQueue(){
40+
Object o = elementData[size-1];
41+
removeElement(0);
42+
return o;
43+
}
44+
/**
45+
* 删除指定索引处元素
46+
* @param index 索引
47+
*/
48+
private void removeElement(int index) {
49+
if(index >= 0){
50+
System.arraycopy(elementData, index+1, elementData, 0, size-index-1);
51+
elementData[--size] = null;
52+
}
53+
}
54+
/**
55+
* 判断队列是否为空
56+
* @return Boolean
57+
*/
58+
public boolean isEmpty(){
59+
return size==0;
60+
}
61+
/**
62+
* 返回队列长度
63+
* @return int 队列长度
64+
*/
65+
public int size(){
66+
return this.size;
67+
}
68+
}

0 commit comments

Comments
 (0)