Skip to content

Commit 182865b

Browse files
committed
Practice code & basic data structure implementation onlyliuxin#1
1 parent b300f43 commit 182865b

File tree

5 files changed

+256
-0
lines changed

5 files changed

+256
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.basic.datastructure;
2+
3+
public class ArrayList implements List {
4+
5+
private Object[] elementData;
6+
private int size;
7+
8+
private int enableCapacity;
9+
10+
public ArrayList() {
11+
this.enableCapacity = 10;
12+
this.elementData = new Object[enableCapacity];
13+
}
14+
15+
@Override
16+
public void add(Object o) {
17+
growIfNeeded();
18+
elementData[size] = o;
19+
this.size++;
20+
}
21+
22+
@Override
23+
public void add(int index, Object o) {
24+
rangeCheckForAdd(index);
25+
growIfNeeded();
26+
27+
Object[] tmpObjects = new Object[elementData.length];
28+
System.arraycopy(elementData, 0, tmpObjects, 0, index);
29+
tmpObjects[index] = o;
30+
System.arraycopy(elementData, index, tmpObjects, index + 1, elementData.length - index - 1);
31+
32+
elementData = tmpObjects;
33+
34+
this.size++;
35+
}
36+
37+
@Override
38+
public Object get(int index) {
39+
if (index > size) {
40+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
41+
}
42+
return elementData[index];
43+
}
44+
45+
@Override
46+
public Object remove(int index) {
47+
Object removedObj = this.get(index);
48+
rangeCheck(index);
49+
Object[] tmpObjects = new Object[elementData.length];
50+
51+
System.arraycopy(elementData, 0, tmpObjects, 0, index);
52+
System.arraycopy(elementData, index + 1, tmpObjects, index, elementData.length - index - 1);
53+
54+
elementData = tmpObjects;
55+
56+
return removedObj;
57+
}
58+
59+
@Override
60+
public int size() {
61+
return size;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
for (int i = 0; i < elementData.length; i++) {
67+
System.out.print(elementData[i] + ",");
68+
}
69+
return "";
70+
}
71+
72+
private void rangeCheck(int paramInt) {
73+
if ((paramInt < 0) || (paramInt >= size)) {
74+
throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt));
75+
}
76+
}
77+
78+
private void rangeCheckForAdd(int paramInt) {
79+
if ((paramInt < 0) || (paramInt > size)) {
80+
throw new IndexOutOfBoundsException(outOfBoundsMsg(paramInt));
81+
}
82+
}
83+
84+
private String outOfBoundsMsg(int paramInt) {
85+
return "Index: " + paramInt + ", Size: " + size;
86+
}
87+
88+
private Object[] growIfNeeded() {
89+
if (enableCapacity <= this.size) {
90+
enableCapacity = enableCapacity * 2;
91+
Object[] largerElementData = new Object[enableCapacity];
92+
System.arraycopy(elementData, 0, largerElementData, 0, elementData.length);
93+
elementData = largerElementData;
94+
}
95+
return elementData;
96+
}
97+
98+
public static void main(String[] args) {
99+
ArrayList list = new ArrayList();
100+
for (int i = 1; i <= 11; i++) {
101+
list.add(String.valueOf(i));
102+
}
103+
list.add(10,"test");
104+
list.get(10);
105+
list.remove(10);
106+
System.out.println(list);
107+
}
108+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.basic.datastructure;
2+
3+
public class LinkedList implements List{
4+
private Node first;
5+
private Node last;
6+
7+
private int size;
8+
9+
public void add(Object o){
10+
if(first == null){
11+
first = new Node(o,null);
12+
}
13+
size ++;
14+
}
15+
public void add(int index , Object o){
16+
17+
}
18+
public Object get(int index){
19+
return null;
20+
}
21+
public Object remove(int index){
22+
return null;
23+
}
24+
25+
public int size(){
26+
return size;
27+
}
28+
29+
public void addFirst(Object o){
30+
31+
}
32+
public void addLast(Object o){
33+
34+
}
35+
public Object removeFirst(){
36+
return null;
37+
}
38+
public Object removeLast(){
39+
return null;
40+
}
41+
42+
43+
private static class Node{
44+
Object item;
45+
Node next;
46+
Node(Object item, Node next){
47+
this.item = item;
48+
this.next = next;
49+
}
50+
}
51+
52+
}
53+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.basic.datastructure;
2+
3+
public interface List {
4+
public void add(Object o);
5+
public void add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
10+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.basic.practice;
2+
3+
/**
4+
*
5+
* @author Wu Alvin
6+
* Java Polymorphic Only represent in method level
7+
*
8+
*/
9+
10+
class Fruit{
11+
String name = "Fruit";
12+
public void print(int i){
13+
System.out.println("Fruit" + i);
14+
}
15+
}
16+
17+
18+
class Apple extends Fruit{
19+
String name = "Apple";
20+
public void print(int i){
21+
System.out.println("Apple" + i);
22+
}
23+
}
24+
25+
26+
public class PolymorphicInJava {
27+
28+
public static void main(String[] args) {
29+
Apple apple = new Apple();
30+
apple.print(100);
31+
//Apple100
32+
System.out.println(apple.name);
33+
//Apple
34+
Fruit fruit = apple;
35+
fruit.print(100);
36+
//Apple100
37+
System.out.println(fruit.name);
38+
//Fruit
39+
}
40+
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.basic.practice;
2+
3+
public class ValuePassInJava {
4+
5+
/*
6+
* Pass the Simple value, etc int String...
7+
* Change will NOT happened
8+
*
9+
*/
10+
public static void main(String[] args) {
11+
String s = new String("123");
12+
int i = 1;
13+
changeVal(i);
14+
System.out.println(i);
15+
16+
}
17+
18+
private static void changeVal(int i){
19+
i = 2;
20+
}
21+
22+
/*
23+
* Pass whole OBJECT, but change the Member variable
24+
* Change will happened
25+
*/
26+
27+
/*
28+
public static void main(String[] args) {
29+
Person p = new Person();
30+
p.age = 10;
31+
changeAge(p);
32+
System.out.println(p.age);
33+
}
34+
35+
private static void changeAge(Person p){
36+
p.age = 20;
37+
}
38+
*/
39+
40+
}
41+
42+
class Person{
43+
int age;
44+
}

0 commit comments

Comments
 (0)