-
Notifications
You must be signed in to change notification settings - Fork 641
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 #6 from conf1102/master
Practice code & basic data structure implementation #1
- Loading branch information
Showing
5 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
group20/404130810/src/com/basic/datastructure/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,108 @@ | ||
package com.basic.datastructure; | ||
|
||
public class ArrayList implements List { | ||
|
||
private Object[] elementData; | ||
private int size; | ||
|
||
private int enableCapacity; | ||
|
||
public ArrayList() { | ||
this.enableCapacity = 10; | ||
this.elementData = new Object[enableCapacity]; | ||
} | ||
|
||
@Override | ||
public void add(Object o) { | ||
growIfNeeded(); | ||
elementData[size] = o; | ||
this.size++; | ||
} | ||
|
||
@Override | ||
public void add(int index, Object o) { | ||
rangeCheckForAdd(index); | ||
growIfNeeded(); | ||
|
||
Object[] tmpObjects = new Object[elementData.length]; | ||
System.arraycopy(elementData, 0, tmpObjects, 0, index); | ||
tmpObjects[index] = o; | ||
System.arraycopy(elementData, index, tmpObjects, index + 1, elementData.length - index - 1); | ||
|
||
elementData = tmpObjects; | ||
|
||
this.size++; | ||
} | ||
|
||
@Override | ||
public Object get(int index) { | ||
if (index > size) { | ||
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); | ||
} | ||
return elementData[index]; | ||
} | ||
|
||
@Override | ||
public Object remove(int index) { | ||
Object removedObj = this.get(index); | ||
rangeCheck(index); | ||
Object[] tmpObjects = new Object[elementData.length]; | ||
|
||
System.arraycopy(elementData, 0, tmpObjects, 0, index); | ||
System.arraycopy(elementData, index + 1, tmpObjects, index, elementData.length - index - 1); | ||
|
||
elementData = tmpObjects; | ||
|
||
return removedObj; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
for (int i = 0; i < elementData.length; i++) { | ||
System.out.print(elementData[i] + ","); | ||
} | ||
return ""; | ||
} | ||
|
||
private void rangeCheck(int paramInt) { | ||
if ((paramInt < 0) || (paramInt >= size)) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); | ||
} | ||
} | ||
|
||
private void rangeCheckForAdd(int paramInt) { | ||
if ((paramInt < 0) || (paramInt > size)) { | ||
throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt)); | ||
} | ||
} | ||
|
||
private String outOfBoundsMsg(int paramInt) { | ||
return "Index: " + paramInt + ", Size: " + size; | ||
} | ||
|
||
private Object[] growIfNeeded() { | ||
if (enableCapacity <= this.size) { | ||
enableCapacity = enableCapacity * 2; | ||
Object[] largerElementData = new Object[enableCapacity]; | ||
System.arraycopy(elementData, 0, largerElementData, 0, elementData.length); | ||
elementData = largerElementData; | ||
} | ||
return elementData; | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList list = new ArrayList(); | ||
for (int i = 1; i <= 11; i++) { | ||
list.add(String.valueOf(i)); | ||
} | ||
list.add(10,"test"); | ||
list.get(10); | ||
list.remove(10); | ||
System.out.println(list); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
group20/404130810/src/com/basic/datastructure/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,117 @@ | ||
package com.basic.datastructure; | ||
|
||
public class LinkedList implements List { | ||
private Node first; | ||
private Node last; | ||
|
||
private int size; | ||
|
||
public void add(Object o) { | ||
addLast(o); | ||
} | ||
|
||
public void add(int index, Object o) { | ||
checkRange(index); | ||
if (index == 0) { | ||
addFirst(o); | ||
} else if (index == size) { | ||
addLast(o); | ||
} else { | ||
|
||
} | ||
} | ||
|
||
public Object get(int index) { | ||
checkRange(index); | ||
if(size > 0){ | ||
int loopTimes = 0; | ||
Node p = first; | ||
while(index > loopTimes){ | ||
p = p.next; | ||
loopTimes ++; | ||
} | ||
return p; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public Object remove(int index) { | ||
return null; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
private void addFirst(Object o) { | ||
if (size == 0) { | ||
first = new Node(o); | ||
last = first; | ||
} else { | ||
Node tmpNode = new Node(o); | ||
tmpNode.next = first; | ||
first = tmpNode; | ||
} | ||
size++; | ||
} | ||
|
||
private void addLast(Object o) { | ||
if (size == 0) { | ||
first = new Node(o); | ||
last = first; | ||
} else { | ||
last.next = new Node(o); | ||
last = last.next; | ||
} | ||
size++; | ||
} | ||
|
||
public Object removeFirst() { | ||
Node tmpNode = first; | ||
first = first.next; | ||
size--; | ||
return tmpNode; | ||
} | ||
|
||
public Object removeLast() { | ||
Node tmpNode = last; | ||
|
||
return null; | ||
} | ||
|
||
private void checkRange(int index) { | ||
if (index > size || index < 0) { | ||
throw new IndexOutOfBoundsException("Index: " + index + "Size: " + size); | ||
} | ||
} | ||
|
||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
Node p = first; | ||
while (p != null) { | ||
sb.append(p.item + "\n"); | ||
p = p.next; | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static class Node { | ||
private Object item; | ||
private Node next; | ||
|
||
Node(Object item) { | ||
this.item = item; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
LinkedList list = new LinkedList(); | ||
for (int i = 0; i < 5; i++) { | ||
list.add(i); | ||
} | ||
list.get(5); | ||
|
||
System.out.println(list); | ||
} | ||
} |
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,10 @@ | ||
package com.basic.datastructure; | ||
|
||
public interface List { | ||
public void add(Object o); | ||
public void add(int index, Object o); | ||
public Object get(int index); | ||
public Object remove(int index); | ||
public int size(); | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
group20/404130810/src/com/basic/practice/PolymorphicInJava.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,41 @@ | ||
package com.basic.practice; | ||
|
||
/** | ||
* | ||
* @author Wu Alvin | ||
* Java Polymorphic Only represent in method level | ||
* | ||
*/ | ||
|
||
class Fruit{ | ||
String name = "Fruit"; | ||
public void print(int i){ | ||
System.out.println("Fruit" + i); | ||
} | ||
} | ||
|
||
|
||
class Apple extends Fruit{ | ||
String name = "Apple"; | ||
public void print(int i){ | ||
System.out.println("Apple" + i); | ||
} | ||
} | ||
|
||
|
||
public class PolymorphicInJava { | ||
|
||
public static void main(String[] args) { | ||
Apple apple = new Apple(); | ||
apple.print(100); | ||
//Apple100 | ||
System.out.println(apple.name); | ||
//Apple | ||
Fruit fruit = apple; | ||
fruit.print(100); | ||
//Apple100 | ||
System.out.println(fruit.name); | ||
//Fruit | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
group20/404130810/src/com/basic/practice/ValuePassInJava.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,44 @@ | ||
package com.basic.practice; | ||
|
||
public class ValuePassInJava { | ||
|
||
/* | ||
* Pass the Simple value, etc int String... | ||
* Change will NOT happened | ||
* | ||
*/ | ||
public static void main(String[] args) { | ||
String s = new String("123"); | ||
int i = 1; | ||
changeVal(i); | ||
System.out.println(i); | ||
|
||
} | ||
|
||
private static void changeVal(int i){ | ||
i = 2; | ||
} | ||
|
||
/* | ||
* Pass whole OBJECT, but change the Member variable | ||
* Change will happened | ||
*/ | ||
|
||
/* | ||
public static void main(String[] args) { | ||
Person p = new Person(); | ||
p.age = 10; | ||
changeAge(p); | ||
System.out.println(p.age); | ||
} | ||
private static void changeAge(Person p){ | ||
p.age = 20; | ||
} | ||
*/ | ||
|
||
} | ||
|
||
class Person{ | ||
int age; | ||
} |