Skip to content

Commit 5ba95e7

Browse files
authored
Merge pull request onlyliuxin#22 from jodie-zss/master
15组02月19日作业
2 parents 659fc03 + 67aaf73 commit 5ba95e7

File tree

135 files changed

+9244
-1
lines changed

Some content is hidden

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

135 files changed

+9244
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ hs_err_pid*
2121
#macOS
2222
.DS_Store
2323

24-
.idea/
24+
2525
*.iml
2626
rebel.*
2727
.rebel.*
2828

29+
target
30+

group15/1500_369516660/.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"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

group15/1500_369516660/.gitignore

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

group15/1500_369516660/.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>codingLeaning</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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.arrayList.basic;
2+
/**
3+
* ¶¨ÒåÁËÔöɾ²é·½·¨
4+
* @author Jodie
5+
*
6+
*/
7+
public interface List {
8+
9+
public void add(Object o);
10+
public void add(int index, Object o);
11+
public Object get(int index);
12+
public Object remove(int index);
13+
public String remove(Object o);
14+
public int size();
15+
16+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.arrayList.basic;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
*
7+
* @author Jodie
8+
*
9+
*/
10+
public class SimpleArrayList implements List {
11+
12+
private final static int default_num = 10;//创建数组时,没有定义大小,默认为10
13+
private Object[] elementData;
14+
private int size = 0;//定义数组的大小,真实数组的大小
15+
16+
public SimpleArrayList(){
17+
this(default_num);
18+
}
19+
20+
public SimpleArrayList(int size) {
21+
if(size<0){//判断定义数组的大小是否小于0
22+
throw new IllegalArgumentException();
23+
}else{
24+
elementData = new Object[size];
25+
}
26+
}
27+
28+
/**
29+
* 重写增加函数的方法1
30+
*/
31+
@Override
32+
public void add(Object o) {
33+
//判断是否需要扩容
34+
ifSpaceEnougn(size+1);
35+
elementData[size++]=o;
36+
}
37+
38+
/**
39+
* 重写增加函数的方法2
40+
*/
41+
@Override
42+
public void add(int index, Object o) {
43+
checkIfOut(index);//是否越界
44+
ifSpaceEnougn(size+1);//是否要扩容
45+
System.arraycopy(elementData, index, elementData, index + 1, size-index);//将index的元素及以后的元素向后移一位
46+
}
47+
/**
48+
* 得到指定下标的数据
49+
*/
50+
@Override
51+
public Object get(int index) {
52+
checkIfOut(index);
53+
return elementData[index];
54+
}
55+
/**
56+
* 删除指定下标的数据
57+
*/
58+
@Override
59+
public Object remove(int index) {
60+
Object value = get(index);
61+
int numRemove = size - index - 1;
62+
if(numRemove > 0){
63+
System.arraycopy(elementData, index+1, elementData, index, size - index);//集体向前进一位
64+
}
65+
elementData[--size] = null;
66+
return value;
67+
}
68+
/**
69+
* 删除指定的数据
70+
*/
71+
@Override
72+
public String remove(Object o) {
73+
if(contains(o)){
74+
remove(indexOf(o));
75+
return "删除成功";
76+
}else{
77+
return "要删除的数据不在数组中,删除失败";
78+
}
79+
}
80+
/**
81+
* 判断是否需要扩容
82+
* @param size
83+
*/
84+
private void ifSpaceEnougn(int size) {
85+
if(size>default_num){
86+
exlicitSpace(size);
87+
}
88+
if(size<0){//当size超过Integer.MAX_VALUE时,会变为负数
89+
throw new OutOfMemoryError();
90+
}
91+
}
92+
/**
93+
* 数组扩容方法
94+
* @param
95+
*/
96+
private void exlicitSpace(int size) {
97+
final int max_arrayLength = Integer.MAX_VALUE-8;
98+
int newLength = elementData.length*2;//一次性扩容为原来的两倍,避免频繁的扩容
99+
if(newLength - size<0){
100+
newLength = size;
101+
}
102+
if(newLength > max_arrayLength){//避免扩容后的大小超过最大值
103+
newLength = (size > max_arrayLength ? Integer.MAX_VALUE : max_arrayLength);
104+
}
105+
elementData = Arrays.copyOf(elementData, newLength);
106+
}
107+
108+
/**
109+
* 判断是否越界
110+
* @param index
111+
*/
112+
private void checkIfOut(int index) {
113+
if(index<0 || index>size){
114+
throw new IndexOutOfBoundsException();
115+
}
116+
}
117+
118+
/**
119+
* 找到指定的数的下标
120+
* @param o
121+
* @return
122+
*/
123+
private int indexOf(Object o) {
124+
if(o!=null){
125+
for(int i=0;i<size;i++){
126+
if(elementData[i].equals(o)){
127+
return i;
128+
}
129+
}
130+
}else{
131+
for(int i=0;i<size;i++){
132+
if(elementData[i] == null){
133+
return i;
134+
}
135+
}
136+
}
137+
return -1;
138+
}
139+
140+
/**
141+
* 判断数组中是否存在这个数据
142+
* @param o
143+
* @return
144+
*/
145+
private boolean contains(Object o) {
146+
return indexOf(o) >= 0;
147+
}
148+
149+
@Override
150+
public int size() {
151+
return size;
152+
}
153+
154+
155+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.arrayList.basic;
2+
3+
public class SimpleLinkedList implements List {
4+
5+
private int size;//Êý×éµÄ´óС
6+
private Node head;
7+
8+
@Override
9+
public void add(Object o) {
10+
11+
}
12+
13+
@Override
14+
public void add(int index, Object o) {
15+
16+
}
17+
18+
@Override
19+
public Object get(int index) {
20+
return null;
21+
}
22+
23+
@Override
24+
public Object remove(int index) {
25+
return null;
26+
}
27+
28+
@Override
29+
public String remove(Object o) {
30+
return null;
31+
}
32+
33+
@Override
34+
public int size() {
35+
return 0;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)