Skip to content

Commit

Permalink
java array list
Browse files Browse the repository at this point in the history
  • Loading branch information
fasionchan committed Jul 22, 2019
1 parent 005261d commit d04aa6c
Show file tree
Hide file tree
Showing 19 changed files with 811 additions and 21 deletions.
23 changes: 23 additions & 0 deletions src/java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
12 changes: 6 additions & 6 deletions src/java/base/CommandLineArgs/CommandLineArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Author: fasion
* Created time: 2019-07-20 12:20:13
* Last Modified by: fasion
* Last Modified time: 2019-07-20 12:22:58
* Last Modified time: 2019-07-22 16:03:24
*/

public class CommandLineArgs {
public static void main(String args[]) {
for (int i=0; i<args.length; i++) {
System.out.printf("Args %d: `%s`\n", i, args[i]);
}
}
public static void main(String args[]) {
for (int i=0; i<args.length; i++) {
System.out.printf("Args %d: `%s`\n", i, args[i]);
}
}
}
18 changes: 18 additions & 0 deletions src/java/base/CommandLineArgs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# FileName: Makefile
# Author: Fasion Chan
# @contact: fasionchan@gmail.com
# @version: $Id$
#
# Description:
#
# Changelog:
#

build:
javac CommandLineArgs.java

run:
java CommandLineArgs

clean:
rm -rf *.class
8 changes: 4 additions & 4 deletions src/java/base/HelloWorld/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* Author: fasion
* Created time: 2019-07-20 12:18:01
* Last Modified by: fasion
* Last Modified time: 2019-07-20 12:18:40
* Last Modified time: 2019-07-22 15:54:06
*/

public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello, world!");
}
public static void main(String args[]) {
System.out.println("Hello, world!");
}
}
13 changes: 13 additions & 0 deletions src/java/base/HelloWorld/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: fasion
# Created time: 2019-07-20 14:56:34
# Last Modified by: fasion
# Last Modified time: 2019-07-22 09:27:55

build:
javac HelloWorld.java

run:
java HelloWorld

clean:
rm -rf *.class
43 changes: 43 additions & 0 deletions src/java/base/ObjectOriented/encapsulation/Counter/Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Author: fasion
* Created time: 2019-07-20 15:38:55
* Last Modified by: fasion
* Last Modified time: 2019-07-22 16:03:33
*/

public class Counter {
int count;

public Counter() {
count = 0;
}

public void add() {
count += 1;
}

public void reset() {
count = 0;
}

public int get() {
return count;
}

public static void main(String args[]) {
Counter counter = new Counter();
System.out.printf("Initial: %d\n", counter.get());

counter.add();
System.out.printf("After first add: %d\n", counter.get());

counter.add();
System.out.printf("After second add: %d\n", counter.get());

counter.reset();
System.out.printf("After reset: %d\n", counter.get());

counter.add();
System.out.printf("After another add: %d\n", counter.get());
}
}
13 changes: 13 additions & 0 deletions src/java/base/ObjectOriented/encapsulation/Counter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: fasion
# Created time: 2019-07-21 09:25:54
# Last Modified by: fasion
# Last Modified time: 2019-07-22 09:27:32

build:
javac Counter.java

run:
java Counter

clean:
rm -rf *.class
22 changes: 11 additions & 11 deletions src/java/base/recursion/Fibonacci/Fibonacci.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
* Author: fasion
* Created time: 2019-07-20 12:02:52
* Last Modified by: fasion
* Last Modified time: 2019-07-20 12:16:29
* Last Modified time: 2019-07-22 16:03:47
*/

public class Fibonacci {
public static int fibonacci(int n) {
if (n==1 || n==2) {
return 1;
}
return Fibonacci.fibonacci(n-1) + Fibonacci.fibonacci(n-2);
}
public static int fibonacci(int n) {
if (n==1 || n==2) {
return 1;
}
return Fibonacci.fibonacci(n-1) + Fibonacci.fibonacci(n-2);
}

public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
System.out.println(Fibonacci.fibonacci(n));
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
System.out.println(Fibonacci.fibonacci(n));
}
}
18 changes: 18 additions & 0 deletions src/java/base/recursion/Fibonacci/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# FileName: Makefile
# Author: Fasion Chan
# @contact: fasionchan@gmail.com
# @version: $Id$
#
# Description:
#
# Changelog:
#

build:
javac Fibonacci.java

run:
java Fibonacci

clean:
rm -rf *.class
170 changes: 170 additions & 0 deletions src/java/linear/ArrayList/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Author: fasion
* Created time: 2019-07-20 16:16:06
* Last Modified by: fasion
* Last Modified time: 2019-07-22 13:59:07
*/

public class ArrayList<AnyType> implements Iterable<AnyType> {
private static final int DEFAULT_CAPACITY = 10;

private int listSize;
private AnyType[] listItems;

public ArrayList() {
listItems = (AnyType[]) new Object[DEFAULT_CAPACITY];
listSize = 0;
}

public int size() {
return listSize;
}

public int capacity() {
return listItems.length;
}

public boolean isEmpty() {
return size() == 0;
}

public boolean isFull() {
return size() == capacity();
}

public AnyType get(int idx) {
if (idx < 0 || idx >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
return listItems[idx];
}

public AnyType set(int idx, AnyType newVal) {
if (idx < 0 || idx >= size()) {
throw new ArrayIndexOutOfBoundsException();
}

AnyType old = listItems[idx];
listItems[idx] = newVal;

return old;
}

public void ensureCapacity(int newCapacity) {
if (newCapacity <= capacity()) {
return;
}

AnyType [] oldItems = listItems;

listItems = (AnyType []) new Object[newCapacity];
for (int i = 0; i < size(); i++) {
listItems[i] = oldItems[i];
}
}

public void add(int idx, AnyType x) {
if (size() == capacity()) {
ensureCapacity(capacity() * 2);
}

for (int i = size(); i > idx; i--) {
listItems[i] = listItems[i-1];
}

listItems[idx] = x;
listSize++;
}

public void add(AnyType x) {
add(size(), x);
}

public AnyType remove(int idx) {
if (idx < 0 || idx >= size()) {
throw new ArrayIndexOutOfBoundsException();
}

AnyType value = listItems[idx];

for (int i = idx; i < size() - 1; i++) {
listItems[i] = listItems[i+1];
}

listSize--;

return value;
}

public void print(String hint) {
System.out.printf("%s[", hint);

for (int i = 0; i < listSize; i++) {
System.out.printf("%s ", listItems[i]);
}

System.out.printf("]\n");
}

private class ArrayListIterator implements java.util.Iterator<AnyType> {
private int current = 0;

public boolean hasNext() {
return current < size();
}

public AnyType next() {
if (!hasNext()) {
throw new java.util.NoSuchElementException();
}
return listItems[current++];
}

public void remove() {
ArrayList.this.remove(--current);
}
}

public java.util.Iterator<AnyType> iterator() {
return new ArrayListIterator();
}

public static void main(String args[]) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.print("After init: ");
System.out.println();

list.add(1);
list.add(2);
list.add(3);

list.print("After add 1, 2, 3: ");
System.out.printf("#1 is: %d\n", list.get(1));
System.out.println();

list.add(0, -1);
list.print("After insert -1: ");
System.out.println();

list.remove(1);
list.print("After remove #1: ");
System.out.println();

list.add(4);
list.print("After append 4: ");
System.out.println();

for (int i = 5; i < 20; i++) {
list.add(i);
}

list.print("Finally: ");
System.out.println();

System.out.println("iterate over list");
java.util.Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
13 changes: 13 additions & 0 deletions src/java/linear/ArrayList/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Author: fasion
# Created time: 2019-07-20 14:56:34
# Last Modified by: fasion
# Last Modified time: 2019-07-22 13:28:43

build:
javac ArrayList.java

run:
java ArrayList

clean:
rm -rf *.class

0 comments on commit d04aa6c

Please sign in to comment.