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.
Merge pull request #9 from johnChnia/master
第一周作业
- Loading branch information
Showing
8 changed files
with
838 additions
and
0 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
group24/315863321/src/main/java/com/johnChnia/coding2017/basic/ArrayList.java
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,167 @@ | ||
package com.johnChnia.coding2017.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Created by john on 2017/3/8. | ||
* @// TODO: 2017/3/15 支持泛型 | ||
*/ | ||
|
||
public class ArrayList { | ||
private int[] elementData; | ||
private int size = 0; | ||
|
||
/** | ||
* Constructs an list with the specified initial capacity. | ||
* | ||
* @param initialCapacity | ||
* @throws IllegalArgumentException if the specified initial capacity | ||
* is negative or zero | ||
*/ | ||
public ArrayList(int initialCapacity) { | ||
if (initialCapacity > 0) { | ||
elementData = new int[initialCapacity]; | ||
} else { | ||
throw new IllegalArgumentException("Illegal Capacity: " + | ||
initialCapacity); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the element at the specified position in this list. | ||
* | ||
* @param index index of the element to return | ||
* @return the element at the specified position in this list | ||
* @throws IndexOutOfBoundsException {@inheritDoc} | ||
*/ | ||
public int get(int index) { | ||
rangeCheck(index); | ||
rangeCheckForAdd(index); | ||
return elementData[index]; | ||
} | ||
|
||
|
||
/** | ||
* Appends the specified element to the end of this list. | ||
* | ||
* @param element element to be appended to this list | ||
*/ | ||
public void add(int element) { | ||
ensureCapacityInternal(size + 1); | ||
elementData[size++] = element; | ||
} | ||
|
||
|
||
/** | ||
* Inserts the specified element at the specified position in this | ||
* list. Shifts the element currently at that position (if any) and | ||
* any subsequent elements to the right (adds one to their indices). | ||
* | ||
* @param element element to be inserted | ||
* @param index index at which the specified element is to be inserted | ||
* @throws IndexOutOfBoundsException {@inheritDoc} | ||
*/ | ||
public void add(int element, int index) { | ||
rangeCheckForAdd(index); | ||
ensureCapacityInternal(size + 1); | ||
System.arraycopy(elementData, index, elementData, index + 1, | ||
size - index); | ||
elementData[index] = element; | ||
size++; | ||
} | ||
|
||
/** | ||
* Removes the element at the specified position in this list. | ||
* Shifts any subsequent elements to the left (subtracts one from their | ||
* indices). | ||
* | ||
* @param index the index of the element to be removed | ||
* @return the element that was removed from the list | ||
* @throws IndexOutOfBoundsException {@inheritDoc} | ||
*/ | ||
public int remove(int index) { | ||
rangeCheckForAdd(index); | ||
int oldValue = elementData[index]; | ||
int numMoved = size() - index - 1; | ||
if (numMoved > 0) { | ||
System.arraycopy(elementData, index + 1, elementData, index, | ||
numMoved); | ||
} | ||
elementData[--size] = 0; // let jc to clear | ||
return oldValue; | ||
} | ||
|
||
/** | ||
* Returns <tt>true</tt> if this list contains no elements. | ||
* | ||
* @return <tt>true</tt> if this list contains no elements | ||
*/ | ||
public boolean empty() { | ||
return size == 0; | ||
} | ||
|
||
/** | ||
* Returns the number of elements in this list. | ||
* | ||
* @return the number of elements in this list | ||
*/ | ||
public int size() { | ||
return size; | ||
} | ||
|
||
|
||
/** | ||
* Increases the capacity to ensure that it can hold at least the | ||
* number of elements specified by the double length of list. | ||
*/ | ||
private void grow() { | ||
elementData = Arrays.copyOf(elementData, 2 * elementData.length); | ||
} | ||
|
||
public String toString() { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append("["); | ||
for (int index = 0; index < size(); index++) { | ||
stringBuilder.append(elementData[index]); | ||
stringBuilder.append(", "); | ||
} | ||
stringBuilder.append("]"); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
private void ensureCapacityInternal(int minCapacity) { | ||
if (minCapacity - elementData.length > 0) | ||
grow(); | ||
} | ||
|
||
/** | ||
* A version of rangeCheck used by add and addAll. | ||
*/ | ||
private void rangeCheckForAdd(int index) { | ||
if (index > elementData.length - 1 || index < 0) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
} | ||
|
||
/** | ||
* Constructs an IndexOutOfBoundsException detail message. | ||
* Of the many possible refactorings of the error handling code, | ||
* this "outlining" performs best with both server and client VMs. | ||
*/ | ||
private String outOfBoundsMsg(int index) { | ||
return "Index: " + index + ", Size: " + elementData.length; | ||
} | ||
|
||
/** | ||
* Checks if the given index is in range. If not, throws an appropriate | ||
* runtime exception. This method does *not* check if the index is | ||
* negative: It is always used immediately prior to an array access, | ||
* which throws an ArrayIndexOutOfBoundsException if index is negative. | ||
*/ | ||
private void rangeCheck(int index) { | ||
if (index >= size) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
} | ||
|
||
} |
231 changes: 231 additions & 0 deletions
231
group24/315863321/src/main/java/com/johnChnia/coding2017/basic/LinkedList.java
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,231 @@ | ||
package com.johnChnia.coding2017.basic; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Created by john on 2017/3/9. | ||
* | ||
* @// TODO: 2017/3/15 支持泛型 | ||
*/ | ||
|
||
public class LinkedList { | ||
|
||
private Node first = null; | ||
private int size = 0; | ||
|
||
/** | ||
* Constructs an empty list. | ||
*/ | ||
public LinkedList() { | ||
|
||
} | ||
|
||
private static class Node { | ||
int element; | ||
Node next; | ||
Node prev; | ||
} | ||
|
||
/** | ||
* Appends the specified element to the end of this list. | ||
* | ||
* @param element element to be appended to this list | ||
*/ | ||
public void add(int element) { | ||
Node newNode = new Node(); | ||
if (first == null) { | ||
addWhenListIsEmpty(newNode, element); | ||
return; | ||
} | ||
Node last = first; | ||
while (last.next != null) | ||
last = last.next; | ||
last.next = newNode; | ||
newNode.prev = last; | ||
newNode.next = null; | ||
newNode.element = element; | ||
size++; | ||
} | ||
|
||
private void addWhenListIsEmpty(Node newNode, int element) { | ||
first = newNode; | ||
first.element = element; | ||
first.next = null; | ||
first.prev = null; | ||
size++; | ||
} | ||
|
||
/** | ||
* Inserts the specified element at the beginning of this list. | ||
* | ||
* @param element the element to add | ||
*/ | ||
public void addFirst(int element) { | ||
Node newNode = new Node(); | ||
if (first == null) { | ||
addWhenListIsEmpty(newNode, element); | ||
return; | ||
} | ||
newNode.next = first; | ||
newNode.prev = null; | ||
newNode.element = element; | ||
|
||
first.prev = newNode; | ||
first = newNode; | ||
size++; | ||
} | ||
|
||
|
||
/** | ||
* Inserts the specified element at the specified position in this list. | ||
* Shifts the element currently at that position (if any) and any | ||
* subsequent elements to the right (adds one to their indices). | ||
* | ||
* @param index index at which the specified element is to be inserted. | ||
* @param element element to be inserted. | ||
* @throws RuntimeException if list size less than 2. | ||
*/ | ||
public void add(int index, int element) { | ||
if (size() < 2) | ||
throw new RuntimeException("list size should greater than or equal to 2"); | ||
isElementIndex(index); | ||
if (index == 0) { | ||
addFirst(element); | ||
return; | ||
} else { | ||
Node temp = new Node(); | ||
Node temp2 = first; | ||
for (int i = 0; i < index; i++) { | ||
temp2 = temp2.next; | ||
} | ||
temp2.prev.next = temp; | ||
temp.prev = temp2.prev; | ||
|
||
temp.next = temp2; | ||
temp2.prev = temp; | ||
temp.element = element; | ||
} | ||
size++; | ||
|
||
} | ||
|
||
|
||
/** | ||
* remove last element in the list. | ||
* | ||
* @throws RuntimeException if the list is empty. | ||
*/ | ||
public void remove() { | ||
if (size == 0) | ||
throw new RuntimeException("linkList size should greater than or equal to 1"); | ||
Node next = first.next; | ||
if (next == null) { | ||
first = null; | ||
} else { | ||
Node last = first; | ||
while (last.next != null) | ||
last = last.next; | ||
last.prev.next = null; | ||
last = null; // help GC | ||
} | ||
size--; | ||
} | ||
|
||
|
||
/** | ||
* Removes and returns the first element from this list. | ||
* | ||
* @return the first element from this list | ||
*/ | ||
public int removeFirst() { | ||
Node f = first; | ||
if (f == null) | ||
throw new NoSuchElementException(); | ||
int element = f.element; | ||
Node next = first.next; | ||
first.element = 0; | ||
first.next = null; // help GC | ||
|
||
first = next; | ||
if (next != null) { | ||
next.prev = null; | ||
} | ||
size--; | ||
return element; | ||
} | ||
|
||
/** | ||
* Returns the element at the specified position in this list. | ||
* | ||
* @param index index of the element to return | ||
* @return the element at the specified position in this list | ||
*/ | ||
public int get(int index) { | ||
checkElementIndex(index); | ||
Node node = first; | ||
if (index == 0) { | ||
return first.element; | ||
} | ||
for (int i = 0; i < index; i++) { | ||
node = node.next; | ||
} | ||
return node.element; | ||
} | ||
|
||
/** | ||
* Returns the first element in this list. | ||
* | ||
* @return the first element in this list | ||
* @throws NoSuchElementException if this list is empty | ||
*/ | ||
public int getFirst() { | ||
final Node f = first; | ||
if (f == null) | ||
throw new NoSuchElementException(); | ||
return f.element; | ||
} | ||
|
||
/** | ||
* Returns the number of elements in this list. | ||
* | ||
* @return the number of elements in this list | ||
*/ | ||
public int size() { | ||
return size; | ||
} | ||
|
||
private void checkElementIndex(int index) { | ||
if (!isElementIndex(index)) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Tells if the argument is the index of an existing element. | ||
*/ | ||
private boolean isElementIndex(int index) { | ||
return index >= 0 && index < size; | ||
} | ||
|
||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(first.element); | ||
Node temp = first; | ||
while (temp.next != null) { | ||
temp = temp.next; | ||
sb.append("→"); | ||
sb.append(temp.element); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* Constructs an IndexOutOfBoundsException detail message. | ||
* Of the many possible refactorings of the error handling code, | ||
* this "outlining" performs best with both server and client VMs. | ||
*/ | ||
private String outOfBoundsMsg(int index) { | ||
return "Index: " + index + ", Size: " + size; | ||
} | ||
} |
Oops, something went wrong.