Skip to content

Commit 0ef1bac

Browse files
author
leijing
committed
创建764189149的目录
创建764189149的目录
1 parent d4a25d6 commit 0ef1bac

File tree

6 files changed

+360
-0
lines changed

6 files changed

+360
-0
lines changed

group07/764189149/.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>

group07/764189149/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.ear
17+
*.zip
18+
*.tar.gz
19+
*.rar
20+
21+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22+
hs_err_pid*
23+
/bin/

group07/764189149/.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>764189149Learning</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: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package firstHomeWork.util;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* @Description: 基于数组的列表
7+
* @author: leijing
8+
* @date: 2017年2月21日 下午9:03:17
9+
* @param <E>
10+
*/
11+
public class ArrayList<E> implements List<E> {
12+
private static int initialCapacity = 10 ;//数组默认初始容量
13+
Object[] elements;//元素的数组
14+
private int size;//元素的个数
15+
16+
public ArrayList(){
17+
this(initialCapacity);
18+
}
19+
public ArrayList(int capacity){
20+
elements = new Object[capacity];
21+
}
22+
private void ensureCapacity(int minCapacity){
23+
if(minCapacity > 0){
24+
25+
}
26+
}
27+
@Override
28+
public boolean add(E e) {
29+
ensureCapacity(size + 1);
30+
elements[size++] = e;
31+
return true;
32+
}
33+
34+
@Override
35+
public E remove(int index) {
36+
rangeCheck(index);
37+
E oldElement = (E) elements[index];
38+
//将其后的元素前移
39+
int needMovedNum = size - index - 1;
40+
move(elements, index+1, elements,index, needMovedNum);
41+
size--;
42+
return oldElement;
43+
}
44+
45+
/**
46+
* @Description: 移动数组中的元素
47+
* @param src 原数组
48+
* @param from 复制元素起始下标
49+
* @param dest 目标元素数组
50+
* @param num 要复制的元素个数
51+
* @return: void
52+
* @author: leijing
53+
* @date: 2017年2月22日 下午7:54:08
54+
*/
55+
private void move(Object[] src , int srcPosition , Object[] dest , int destPosition, int num){
56+
for(int i = 0 ; i < num ; i ++){
57+
dest[destPosition++] = src[srcPosition++];
58+
}
59+
}
60+
61+
/**
62+
* @Description: 检查下标是否正确,如果越界抛出异常
63+
* @param index
64+
* @return: void
65+
* @author: leijing
66+
* @date: 2017年2月22日 下午7:52:59
67+
*/
68+
private void rangeCheck(int index){
69+
if(index < 0 || index > size){
70+
throw new IndexOutOfBoundsException();
71+
}
72+
}
73+
74+
@Override
75+
public boolean remove(Object o) {
76+
if(o == null){
77+
for (int index = 0; index < size; index++) {
78+
if(elements[index] == null){
79+
remove(index);
80+
return true;
81+
}
82+
}
83+
}else{
84+
for (int index = 0; index < size; index++) {
85+
if(o.equals(elements[index])){
86+
remove(index);
87+
return true;
88+
}
89+
}
90+
}
91+
return false;
92+
}
93+
94+
@Override
95+
public int size() {
96+
return size;
97+
}
98+
99+
@Override
100+
public boolean isEmpty() {
101+
return size == 0;
102+
}
103+
104+
@Override
105+
public E get(int index) {
106+
rangeCheck(index);
107+
return (E) elements[index];
108+
}
109+
110+
@Override
111+
public E set(int index, E e) {
112+
rangeCheck(index);
113+
E oldElement = (E) elements[index];
114+
elements[index] = e;
115+
return oldElement;
116+
}
117+
118+
@Override
119+
public boolean contains(Object o) {
120+
return indexOf(o) >= 0;
121+
}
122+
123+
private int indexOf(Object o){
124+
if(o == null){
125+
for (int index = 0; index < size; index++) {
126+
if(elements[index] == null){
127+
return index;
128+
}
129+
}
130+
}else{
131+
for (int index = 0; index < size; index++) {
132+
if(o.equals(elements[index])){
133+
return index;
134+
}
135+
}
136+
}
137+
return -1;
138+
}
139+
140+
@Override
141+
public void clear() {
142+
for (int index = 0; index < size; index++) {
143+
elements[index] = null;
144+
}
145+
size = 0;
146+
}
147+
148+
@Override
149+
public Iterator<E> iterator() {
150+
return new ArraylistIterator();
151+
}
152+
153+
@Override
154+
public String toString() {
155+
StringBuilder sb = new StringBuilder();
156+
for (int index = 0; index < size; index++) {
157+
if(index == size -1){
158+
sb.append(elements[index]);
159+
}else{
160+
sb.append(elements[index]).append(",");
161+
}
162+
163+
}
164+
return sb.toString();
165+
}
166+
private class ArraylistIterator implements Iterator<E>{
167+
private int position;
168+
169+
@Override
170+
public boolean hasNext() {
171+
return position != size;
172+
}
173+
174+
@Override
175+
public E next() {
176+
Object[] elements = ArrayList.this.elements;
177+
int i = position;
178+
if(i >= size){
179+
throw new NoSuchElementException();
180+
}
181+
position = i + 1;
182+
return (E) elements[i+1];
183+
}
184+
185+
@Override
186+
public void remove() {
187+
if(position > size){
188+
throw new NoSuchElementException();
189+
}
190+
ArrayList.this.remove(position);
191+
}
192+
193+
}
194+
195+
196+
197+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package firstHomeWork.util;
2+
3+
/**
4+
* @Description: 迭代器
5+
* @author: leijing
6+
* @date: 2017年2月21日 下午8:49:10
7+
* @param <E>
8+
*/
9+
public interface Iterator<E> {
10+
/**
11+
* @Description: 返回迭代器中是否有下一个元素
12+
* @return: boolean
13+
* @author: leijing
14+
* @date: 2017年2月21日 下午8:49:52
15+
*/
16+
boolean hasNext();
17+
/**
18+
* @Description: 返回迭代器中的下一个元素
19+
* @return: E
20+
* @author: leijing
21+
* @date: 2017年2月21日 下午8:50:35
22+
*/
23+
E next();
24+
/**
25+
* @Description: 删除迭代器中的当前元素
26+
* @author: leijing
27+
* @date: 2017年2月21日 下午8:51:07
28+
*/
29+
void remove();
30+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package firstHomeWork.util;
2+
3+
/**
4+
* @Description: 定义一组操作有序列表的接口
5+
* @author: leijing
6+
* @date: 2017年2月21日 下午8:53:52
7+
* @param <E>
8+
*/
9+
public interface List<E> {
10+
/**
11+
* @Description: 添加元素
12+
* @param e
13+
* @return: boolean
14+
* @author: leijing
15+
* @date: 2017年2月21日 下午8:55:32
16+
*/
17+
boolean add(E e);
18+
/**
19+
* @Description: 删除指定索引的元素
20+
* @param index
21+
* @return: E
22+
* @author: leijing
23+
* @date: 2017年2月21日 下午8:56:08
24+
*/
25+
E remove(int index);
26+
/**
27+
* @Description: 删除元素
28+
* @param o
29+
* @return: boolean
30+
* @author: leijing
31+
* @date: 2017年2月21日 下午8:56:28
32+
*/
33+
boolean remove(Object o);
34+
/**
35+
* @Description: 返回元素个数
36+
* @return: int
37+
* @author: leijing
38+
* @date: 2017年2月21日 下午8:57:25
39+
*/
40+
int size();
41+
/**
42+
* @Description: 判断集合是否为空
43+
* @return: boolean
44+
* @author: leijing
45+
* @date: 2017年2月21日 下午8:57:51
46+
*/
47+
boolean isEmpty();
48+
/**
49+
* @Description: 获取指定位置的元素
50+
* @param index
51+
* @return: E
52+
* @author: leijing
53+
* @date: 2017年2月21日 下午8:58:27
54+
*/
55+
E get(int index);
56+
/**
57+
* @Description: 设置指定位置的元素
58+
* @param index
59+
* @param e
60+
* @return: E
61+
* @author: leijing
62+
* @date: 2017年2月21日 下午8:58:58
63+
*/
64+
E set(int index , E e);
65+
/**
66+
* @Description: 判断集合是否包含某个元素
67+
* @param o
68+
* @return: boolean
69+
* @author: leijing
70+
* @date: 2017年2月21日 下午8:59:32
71+
*/
72+
boolean contains(Object o);
73+
/**
74+
* @Description: 清空集合
75+
* @return: void
76+
* @author: leijing
77+
* @date: 2017年2月21日 下午9:00:12
78+
*/
79+
void clear();
80+
/**
81+
* @Description: 获取集合的迭代器
82+
* @return: Iterator<E>
83+
* @author: leijing
84+
* @date: 2017年2月21日 下午9:00:47
85+
*/
86+
Iterator<E> iterator();
87+
}

0 commit comments

Comments
 (0)