Skip to content

Commit 758fb4c

Browse files
AdministratorAdministrator
Administrator
authored and
Administrator
committed
作业提交
1 parent b3edbe4 commit 758fb4c

File tree

3 files changed

+90
-21
lines changed

3 files changed

+90
-21
lines changed
Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,78 @@
11
package com.coding.basic;
22

3+
import com.sun.media.sound.EmergencySoundbank;
4+
35
public class ArrayList implements List {
4-
6+
/**
7+
* 列表中元素的个数
8+
*/
59
private int size = 0;
610

7-
private Object[] elementData = new Object[100];
11+
/**
12+
* 初始化数组大小
13+
*/
14+
private int arraySize = 100;
15+
/**
16+
* 初始化数组
17+
*/
18+
private Object[] elementData = new Object[arraySize];
819

20+
/**
21+
* 添加方法
22+
*/
923
public void add(Object o){
10-
24+
if(size>=(arraySize*0.75)){
25+
Object [] target = new Object[(int) (arraySize*1.5)];
26+
System.arraycopy(elementData,0,target,0,arraySize);
27+
target[size-1] = o;
28+
size++;
29+
}else if(size<(arraySize*0.75)){
30+
elementData[size-1]=o;
31+
size++;
32+
}
1133
}
34+
/**
35+
* 根据索引添加方法
36+
*/
1237
public void add(int index, Object o){
13-
38+
if(size >= arraySize*0.75){
39+
Object [] target = new Object[(int) (arraySize*1.5)];
40+
System.arraycopy(elementData,0,target,0,arraySize);
41+
for (int j = target.length;j>=index;j--){
42+
target[j-1] = target[j-2];
43+
}
44+
target[index] = o;
45+
size++;
46+
}else if(size < arraySize*0.75){
47+
for (int j = elementData.length;j>=index;j--){
48+
elementData[j-1] = elementData[j-2];
49+
}
50+
elementData[index] = o;
51+
size++;
52+
}
1453
}
15-
54+
/**
55+
* 根据索引获取对象
56+
*/
1657
public Object get(int index){
17-
return null;
58+
return elementData[index];
1859
}
19-
60+
/**
61+
* 根据索引移除对象
62+
*/
2063
public Object remove(int index){
21-
return null;
64+
for (int i = index; i < elementData.length; i++) {
65+
elementData[i]=elementData[i+1];
66+
size++;
67+
}
68+
return elementData[index];
2269
}
23-
70+
/**
71+
* 获取数组大小
72+
*/
2473
public int size(){
25-
return -1;
74+
return this.size;
2675
}
2776

28-
public Iterator iterator(){
29-
return null;
30-
}
3177

3278
}

group20/423184723/src/basic/Queue.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22

33
public class Queue {
44

5-
public void enQueue(Object o){
5+
private LinkedList list = new LinkedList();
6+
public void enQueue(Object o){
7+
list.add(o);
68
}
79

810
public Object deQueue(){
9-
return null;
11+
int length = list.size();
12+
if (length == 0) {
13+
return null;
14+
}
15+
return list.removeFirst();
1016
}
1117

1218
public boolean isEmpty(){
13-
return false;
19+
if (list.size() == 0) {
20+
return true;
21+
} else {
22+
return false;
23+
}
1424
}
1525

1626
public int size(){
17-
return -1;
27+
return list.size;
1828
}
1929
}

group20/423184723/src/basic/Stack.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@ public class Stack {
44
private ArrayList elementData = new ArrayList();
55

66
public void push(Object o){
7+
elementData.add(o);
78
}
89

910
public Object pop(){
10-
return null;
11+
int length = elementData.size();
12+
if (length == 0) {
13+
return null;
14+
}
15+
return elementData.remove(length - 1);
1116
}
1217

1318
public Object peek(){
14-
return null;
19+
int length = elementData.size();
20+
if (length == 0) {
21+
return null;
22+
}
23+
return elementData.get(length - 1);
1524
}
1625
public boolean isEmpty(){
17-
return false;
26+
if (elementData.size() != 0) {
27+
return false;
28+
} else {
29+
return true;
30+
}
1831
}
1932
public int size(){
20-
return -1;
33+
return elementData.size();
2134
}
2235
}

0 commit comments

Comments
 (0)