forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
BlindingDark
committed
Mar 11, 2017
1 parent
9beee2d
commit c4e4c7c
Showing
3 changed files
with
76 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,73 @@ | ||
package Collection; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
|
||
if(o==null) | ||
throw new RuntimeException("插入的值不能为null"); | ||
if((size+1)>=100){ | ||
throw new RuntimeException("数组越界异常"); | ||
} | ||
elementData[size]=o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
|
||
for(int i=index;i<size;i++){ | ||
elementData[i+1]=elementData[i]; | ||
} | ||
elementData[index]=o; | ||
} | ||
|
||
public Object get(int index){ | ||
|
||
if(index<0 || index>=size) | ||
throw new RuntimeException("索引异常"); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
|
||
if(index<0 || index>=size) | ||
throw new RuntimeException("索引异常"); | ||
Object temp=elementData[index]; | ||
for(int i=index;i<size-1;i++){ | ||
elementData[i]=elementData[i+1]; | ||
} | ||
return temp; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator{ | ||
|
||
private int cursor=0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
|
||
return cursor!=size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
int i=cursor; | ||
cursor=i+1; | ||
return elementData[i]; | ||
} | ||
|
||
} | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o) { | ||
|
||
if (o == null) | ||
throw new RuntimeException("插入的值不能为null"); | ||
if ((size + 1) >= 100) { | ||
throw new RuntimeException("数组越界异常"); | ||
} | ||
elementData[size] = o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
|
||
for (int i = index; i < size; i++) { | ||
elementData[i + 1] = elementData[i]; | ||
} | ||
elementData[index] = o; | ||
} | ||
|
||
public Object get(int index) { | ||
|
||
if (index < 0 || index >= size) | ||
throw new RuntimeException("索引异常"); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
|
||
if (index < 0 || index >= size) | ||
throw new RuntimeException("索引异常"); | ||
Object temp = elementData[index]; | ||
for (int i = index; i < size - 1; i++) { | ||
elementData[i] = elementData[i + 1]; | ||
} | ||
return temp; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new Itr(); | ||
} | ||
|
||
private class Itr implements Iterator { | ||
|
||
private int cursor = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
|
||
return cursor != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
int i = cursor; | ||
cursor = i + 1; | ||
return elementData[i]; | ||
} | ||
|
||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package zhuakuang.homework.first; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
System.out.println(1); | ||
} | ||
} |