forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from CottonChou/master
398129523
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.coding.work; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
|
||
|
||
public class ArrayList<E> { | ||
private Object[] elementData; | ||
private int size; | ||
|
||
|
||
public ArrayList(int init) { | ||
// TODO Auto-generated constructor stub | ||
if (init <= 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.elementData = new Object[init]; | ||
} | ||
|
||
public ArrayList(){ | ||
this(10); //调用重载的构造函数 | ||
} | ||
|
||
public void changeCapacity(int curCapacity) { | ||
int oldCapacity = elementData.length; | ||
if(curCapacity > oldCapacity){ | ||
Object[] oldData = elementData; | ||
int newCapacity = oldCapacity + oldCapacity >> 1; //右移一位除以2,相当于扩 扩大到1.5倍 | ||
if (newCapacity <curCapacity ) { | ||
newCapacity = curCapacity; | ||
} | ||
elementData = Arrays.copyOf(elementData, newCapacity);//Arrys.copyOf返回一个新的数组 | ||
} | ||
} | ||
|
||
public boolean add(E e) { | ||
changeCapacity(size + 1); | ||
elementData[size++] = e; | ||
return true; | ||
} | ||
|
||
public boolean add(int index, E e) { | ||
if (index < 0) { | ||
throw new IllegalArgumentException(); | ||
} | ||
if (index > elementData.length - 1) { | ||
changeCapacity(index + 1); | ||
} | ||
elementData[index] = e; | ||
size++; | ||
return true; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public E get(int index) { | ||
if (index <= size - 1) { | ||
return (E) elementData[index]; | ||
|
||
}else { | ||
return null; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public E remove(int index) { | ||
if (index >= 0 && index <= size - 1) { | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); | ||
size--; | ||
return (E) elementData[index]; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
|
||
|
||
public int size() { | ||
return size;//全局变量 | ||
} | ||
|
||
//不会实现 | ||
public Iterator iterator() { | ||
return null; | ||
|
||
} | ||
|
||
} |
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,46 @@ | ||
package com.coding.work; | ||
|
||
public class LinkedList{ | ||
private int size; | ||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
} | ||
private Node head = new Node(); | ||
private Node lastNode; | ||
|
||
public LinkedList(){ | ||
head.next = null; | ||
head.data = null; | ||
lastNode = head; | ||
} | ||
|
||
public void add(Object o) { | ||
Node curnode = new Node(); | ||
curnode.data = o; | ||
curnode.next = null; | ||
if (head.next == null) { | ||
head.next = curnode; | ||
lastNode = curnode; | ||
size++; | ||
}else { | ||
lastNode.next = curnode; | ||
size++; | ||
} | ||
|
||
|
||
} | ||
public void add(int index , Object o) { | ||
if (index > size - 1) { | ||
for(int i = size - 1; i <= index; i++){ | ||
Node curnode = new Node(); | ||
lastNode.next = curnode; | ||
lastNode = curnode; | ||
} | ||
lastNode.data = o; | ||
} | ||
|
||
} | ||
|
||
} |