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 #2 from Greastate/master
123
- Loading branch information
Showing
12 changed files
with
495 additions
and
2 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,111 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Created by huitailang on 17/2/25. | ||
* | ||
* @author zhangkun | ||
* @date 2017年02月25日13:23:30 | ||
*/ | ||
public class ArrayList implements List { | ||
private int size = 0; | ||
private static final int DEFAULT_SIZE = 16; | ||
private Object[] elementData = null; | ||
private int index; | ||
|
||
public ArrayList() { | ||
elementData = new Object[DEFAULT_SIZE]; | ||
} | ||
|
||
public ArrayList(final int size) { | ||
elementData = new Object[size]; | ||
} | ||
|
||
public void add(Object o) { | ||
//如果当前元素个数大于数组长度的2/3 | ||
if (size() > elementData.length * 2 / 3) { | ||
raiseArray(); | ||
} | ||
|
||
elementData[index++] = o; | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
checkParam(index); | ||
|
||
//如果当前元素个数大于数组长度的2/3 | ||
if (size() > elementData.length * 2 / 3) { | ||
raiseArray(); | ||
} | ||
|
||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
checkParam(index); | ||
|
||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
checkParam(index); | ||
|
||
Object o = elementData[index]; | ||
elementData[index] = null; | ||
size--; | ||
return o; | ||
} | ||
|
||
private void raiseArray() { | ||
Object[] newElementData = Arrays.copyOf(elementData, size() * 2); | ||
elementData = newElementData; | ||
} | ||
|
||
private void checkParam(int index) { | ||
if (index < 0 || index > elementData.length - 1) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ListIterator(); | ||
} | ||
|
||
private class ListIterator implements Iterator{ | ||
int cursor; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor != size; | ||
} | ||
|
||
public void remove(){ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if(!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
int i = cursor; | ||
if (i >= size) | ||
throw new NoSuchElementException(); | ||
|
||
Object[] elementData = ArrayList.this.elementData; | ||
|
||
cursor = i + 1; | ||
|
||
return elementData[i]; | ||
} | ||
} | ||
} |
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,74 @@ | ||
package com.coding.basic; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by huitailang on 17/2/25. | ||
* test arraylist | ||
*/ | ||
public class ArrayListTest { | ||
ArrayList arrayList = null; | ||
|
||
@Before | ||
public void setUp() { | ||
arrayList = new ArrayList(); | ||
} | ||
|
||
@Test | ||
public void testArrayLength() { | ||
int[] array = new int[10]; | ||
Assert.assertEquals(10, array.length); | ||
} | ||
|
||
@Test | ||
public void testAddElement() { | ||
arrayList.add(11); | ||
Assert.assertEquals(11, arrayList.get(0)); | ||
printElementSize(arrayList); | ||
|
||
for (int i = 0; i < 18; i++) { | ||
|
||
} | ||
} | ||
|
||
@Test | ||
public void testAriseArray() { | ||
for (int i = 0; i < 18; i++) { | ||
arrayList.add(i + 1); | ||
} | ||
|
||
Assert.assertEquals(18, arrayList.size()); | ||
|
||
for (int i = 0; i < 18; i++) { | ||
System.out.println(arrayList.get(i)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRemoveElement() { | ||
for (int i = 0; i < 18; i++) { | ||
arrayList.add(i + 1); | ||
} | ||
|
||
Assert.assertEquals(18, arrayList.size()); | ||
|
||
arrayList.remove(17); | ||
|
||
Assert.assertEquals(17, arrayList.size()); | ||
|
||
for (int i = 0; i < 18; i++) { | ||
System.out.println(arrayList.get(i)); | ||
} | ||
} | ||
|
||
@Test(expected = IndexOutOfBoundsException.class) | ||
public void testInValidGet() { | ||
arrayList.get(19); | ||
} | ||
|
||
private void printElementSize(ArrayList arrayList) { | ||
System.out.println("array size => " + arrayList.size()); | ||
} | ||
} |
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,13 @@ | ||
package com.coding.basic; | ||
|
||
/** | ||
* Created by huitailang on 17/2/25. | ||
* | ||
* @author zhangkun | ||
* @date 2017年02月25日16:25:42 | ||
*/ | ||
public interface Iterator { | ||
public boolean hasNext(); | ||
|
||
public Object next(); | ||
} |
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,190 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Created by huitailang on 17/2/25. | ||
* | ||
* @author zhangkun | ||
* @date 2017年02月25日13:57:58 | ||
*/ | ||
public class LinkedList implements List { | ||
private Node head; | ||
private int size; | ||
|
||
public LinkedList() { | ||
head = null; | ||
size = 0; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
if (head == null) { | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
head = newNode; | ||
} | ||
|
||
Node oldhead = head; | ||
head = new Node(); | ||
head.data = o; | ||
head.next = oldhead; | ||
size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
Node newNode = new Node(); | ||
newNode.data = o; | ||
|
||
if (head == null) { | ||
head = newNode; | ||
} | ||
|
||
if (index < 1 || index > size + 1) { | ||
throw new IllegalArgumentException("invalid index, it's should be 1 and" + size + 1); | ||
} | ||
|
||
if (index == 1) { | ||
newNode.next = head; | ||
} else { | ||
Node currentNode = head; | ||
int count = 1; | ||
while (count < index - 1) { | ||
count++; | ||
currentNode = currentNode.next; | ||
} | ||
newNode.next = currentNode.next; | ||
currentNode.next = newNode; | ||
} | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
if (head == null) { | ||
return null; | ||
} | ||
|
||
if (index == 1) { | ||
return head.next.data; | ||
} else { | ||
Node currentNode = head; | ||
int count = 1; | ||
while (count < index - 1) { | ||
count++; | ||
currentNode = currentNode.next; | ||
} | ||
|
||
return currentNode.next.data; | ||
} | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
Object result = null; | ||
|
||
if (index < 1 || index > size) { | ||
throw new IllegalArgumentException("invalid index, it's should be 1 and " + size); | ||
} | ||
|
||
if (index == 1) { | ||
Node currentNode = head.next; | ||
head = null; | ||
return currentNode; | ||
} else { | ||
Node prevNode = head; | ||
|
||
int count = 1; | ||
while (count < index - 1) { | ||
prevNode = prevNode.next; | ||
count++; | ||
} | ||
Node currentNode = prevNode.next; | ||
prevNode.next = currentNode.next; | ||
result = currentNode.data; | ||
currentNode = null; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
add(1, o); | ||
} | ||
|
||
public void addLast(Object o) { | ||
add(size + 1, o); | ||
} | ||
|
||
public Object removeFirst() { | ||
return remove(1); | ||
} | ||
|
||
public Object removeLast() { | ||
return remove(size); | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ListIterator(); | ||
} | ||
|
||
public void print() { | ||
if (head == null) { | ||
System.out.println("No elements in the list!"); | ||
} | ||
|
||
Node currentNode = head; | ||
while (currentNode != null) { | ||
System.out.println(currentNode.data + "->"); | ||
currentNode = currentNode.next; | ||
} | ||
|
||
System.out.println(); | ||
} | ||
|
||
public int length() { | ||
int count = 0; | ||
|
||
Node currentNode = head; | ||
while (currentNode != null) { | ||
count++; | ||
currentNode = currentNode.next; | ||
} | ||
|
||
return count; | ||
} | ||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
} | ||
|
||
private class ListIterator implements Iterator { | ||
private Node current = head; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return current != null; | ||
} | ||
|
||
public void remove() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
Object o = current.data; | ||
current = current.next; | ||
return o; | ||
} | ||
} | ||
} |
Oops, something went wrong.