Skip to content

Commit f12df5d

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 50e4a29 + 59783e0 commit f12df5d

File tree

142 files changed

+5723
-43
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+5723
-43
lines changed

group04/1299310140/src/com/coding/basic/ArrayList.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,35 @@ public int size(){
6464
}
6565

6666
public Iterator iterator(){
67-
return null;
67+
return new ArrayListIterator(this);
68+
}
69+
70+
public static class ArrayListIterator implements Iterator{
71+
private ArrayList list;
72+
private int pres;
73+
74+
public ArrayListIterator(ArrayList list) {
75+
super();
76+
this.list = list;
77+
this.pres = 0;
78+
}
79+
80+
@Override
81+
public boolean hasNext() {
82+
if(this.pres < this.list.size()){
83+
return true;
84+
}else{
85+
return false;
86+
}
87+
}
88+
89+
@Override
90+
public Object next() {
91+
Object o = this.list.get(this.pres);
92+
this.pres++;
93+
return o;
94+
}
95+
6896
}
6997

7098
/*

group04/1299310140/src/com/coding/basic/LinkedList.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,37 @@ public Object removeLast(){
151151
}
152152

153153
public Iterator iterator(){
154-
return null;
154+
return new LinkedListIterator(this);
155155
}
156156

157+
private static class LinkedListIterator implements Iterator{
158+
// private LinkedList list;
159+
private Node pres;
160+
161+
public LinkedListIterator(LinkedList list) {
162+
super();
163+
// this.list = list;
164+
this.pres = list.head;
165+
}
166+
167+
@Override
168+
public boolean hasNext() {
169+
if(this.pres != null){
170+
return true;
171+
}else{
172+
return false;
173+
}
174+
}
175+
176+
@Override
177+
public Object next() {
178+
Object o = this.pres.data;
179+
pres = pres.next;
180+
return o;
181+
}
182+
183+
}
184+
157185
private static class Node{
158186
Object data;
159187
Node next;

group04/1751801281/.classpath

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

group04/1751801281/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

group04/1751801281/.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>1751801281Learning</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: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
import java.util.ConcurrentModificationException;
5+
import java.util.NoSuchElementException;
6+
7+
public class ArrayList implements List {
8+
9+
private int size = 0;
10+
11+
private Object[] elementData = new Object[10];
12+
13+
public void add(Object o) {
14+
// 进行扩容检查
15+
ensureCapacity(size + 1);
16+
elementData[size++] = o;
17+
}
18+
19+
public void add(int index, Object o) {
20+
if (index > size || index < 0)
21+
throw new IndexOutOfBoundsException("数组越界异常");
22+
ensureCapacity(size + 1);
23+
// 对数组进行复制处理,目的就是空出index的位置插入o,并将index后的元素位移一个位置
24+
System.arraycopy(elementData, index, elementData, index + 1, size - index);
25+
elementData[index] = o;
26+
size++;
27+
}
28+
29+
public Object get(int index) {
30+
if (index < 0 || index > elementData.length) {
31+
return null;
32+
} else if (index > size && index < elementData.length) {
33+
return null;
34+
} else {
35+
return elementData[index];
36+
}
37+
}
38+
39+
public Object remove(int index) {
40+
if (index < 0 || index > elementData.length) {
41+
return null;
42+
} else {
43+
int numMoved = size - index - 1;
44+
if (numMoved > 0)
45+
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
46+
elementData[--size] = null;
47+
return elementData[index];
48+
}
49+
}
50+
51+
public int size() {
52+
return size;
53+
}
54+
55+
public void ensureCapacity(int minCapacity) {
56+
if (minCapacity > elementData.length) {
57+
grow(minCapacity);
58+
}
59+
}
60+
61+
public void grow(int minCapacity) {
62+
// 当前数组的长度
63+
int oldCapacity = elementData.length;
64+
// 最小需要的容量大于当前数组的长度则进行扩容
65+
if (minCapacity > oldCapacity) {
66+
Object oldData[] = elementData;
67+
// 新扩容的数组长度为旧容量的1.5倍+1
68+
int newCapacity = (oldCapacity * 3) / 2 + 1;
69+
// 如果新扩容的数组长度还是比最小需要的容量小,则以最小需要的容量为长度进行扩容
70+
if (newCapacity < minCapacity)
71+
newCapacity = minCapacity;
72+
// 进行数据拷贝,Arrays.copyOf底层实现是System.arrayCopy()
73+
elementData = Arrays.copyOf(elementData, newCapacity);
74+
}
75+
}
76+
77+
public Iterator iterator() {
78+
return new ArrayListIterator();
79+
}
80+
81+
private class ArrayListIterator implements Iterator {
82+
int cursor; // index of next element to return
83+
int lastRet = -1; // index of last element returned; -1 if no such
84+
85+
public boolean hasNext() {
86+
return cursor != size;
87+
}
88+
89+
public Object next() {
90+
int i = cursor;
91+
if (i >= size)
92+
throw new NoSuchElementException();
93+
Object[] elementData = ArrayList.this.elementData;
94+
if (i >= elementData.length)
95+
throw new ConcurrentModificationException();
96+
cursor = i + 1;
97+
return elementData[lastRet = i];
98+
}
99+
}
100+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.coding.basic;
2+
3+
import java.util.NoSuchElementException;
4+
5+
public class LinkedList implements List {
6+
7+
private Node head;
8+
private Node tail;
9+
private int size;
10+
11+
private static class Node {
12+
Object data;
13+
Node next;
14+
Node previous;
15+
}
16+
17+
public void add(Object o) {
18+
if (head == null) {
19+
head = new Node();
20+
head.data = o;
21+
tail = head;
22+
} else {
23+
Node oldtail = tail;
24+
tail = new Node();
25+
tail.data = o;
26+
tail.next = null;
27+
tail.previous = oldtail;
28+
oldtail.next = tail;
29+
}
30+
size++;
31+
}
32+
33+
public void add(int index, Object o) {
34+
if (index < 0 || index >= size) {
35+
throw new IndexOutOfBoundsException("数组越界异常");
36+
} else if (index == size) {
37+
Node oldtail = tail;
38+
tail = new Node();
39+
tail.data = o;
40+
tail.previous = oldtail;
41+
oldtail.next = tail;
42+
size++;
43+
} else {
44+
45+
}
46+
size++;
47+
}
48+
49+
public Object get(int index) {
50+
if (index < 0 || index >= size) {
51+
throw new IndexOutOfBoundsException("数组越界异常");
52+
}
53+
for (int i = 0; i < index; i++) {
54+
head = head.next;
55+
}
56+
return head.data;
57+
}
58+
59+
public Object remove(int index) {
60+
61+
return null;
62+
}
63+
64+
public int size() {
65+
return size;
66+
}
67+
68+
public void addFirst(Object o) {
69+
Node oldhead = head;
70+
head = new Node();
71+
head.data = o;
72+
head.next = oldhead;
73+
head.previous = null;
74+
oldhead.previous = head;
75+
size++;
76+
}
77+
78+
public void addLast(Object o) {
79+
Node oldtail = tail;
80+
tail = new Node();
81+
tail.data = o;
82+
tail.previous = oldtail;
83+
tail.next = null;
84+
oldtail.next = tail;
85+
size++;
86+
}
87+
88+
public Object removeFirst() {
89+
Object data = head.data;
90+
head = head.next;
91+
head.previous = null;
92+
size--;
93+
return data;
94+
}
95+
96+
public Object removeLast() {
97+
Object data = tail.data;
98+
tail = tail.previous;
99+
tail.next = null;
100+
size--;
101+
return data;
102+
}
103+
104+
public Iterator iterator() {
105+
106+
return new LinkedListIterator();
107+
}
108+
109+
private class LinkedListIterator implements Iterator {
110+
int cursor;
111+
int lastReset = -1;
112+
113+
@Override
114+
public boolean hasNext() {
115+
return size != cursor;
116+
}
117+
118+
@Override
119+
public Object next() {
120+
int i = cursor;
121+
if (i > size) {
122+
throw new NoSuchElementException();
123+
}
124+
Node n = head;
125+
for (int j = 0; j < i; j++) {
126+
n = n.next;
127+
}
128+
cursor = i + 1;
129+
lastReset = i;
130+
return n.data;
131+
}
132+
}
133+
}
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.coding.basic;
2+
3+
public class Queue {
4+
5+
private int size;
6+
7+
private LinkedList elementData = new LinkedList();
8+
9+
public void enQueue(Object o) {
10+
elementData.add(o);
11+
size++;
12+
}
13+
14+
public Object deQueue() {
15+
size--;
16+
return elementData.removeFirst();
17+
18+
}
19+
20+
public boolean isEmpty() {
21+
return size == 0;
22+
}
23+
24+
public int size() {
25+
return size;
26+
}
27+
}

0 commit comments

Comments
 (0)