ผลต่างระหว่างรุ่นของ "01204212/codes/zooma"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(01204212/FlexiArray ถูกเปลี่ยนชื่อเป็น 01204212/Zooma)
แถว 1: แถว 1:
 
: Back to [[01204212]]
 
: Back to [[01204212]]
  
== FlexiArray.java==
+
== First implemenation with arrays ==
 +
=== FlexiArray.java===
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public class FlexiArray<T> {
 
public class FlexiArray<T> {
แถว 50: แถว 51:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Examples of lambda expression ==
+
=== Examples of lambda expression ===
  
 
Using lambda expression:
 
Using lambda expression:
แถว 66: แถว 67:
 
}
 
}
 
});
 
});
 +
</syntaxhighlight>
 +
 +
== Zooma 1 solution with linked list (ListNode) ==
 +
 +
=== ListNode.java ===
 +
<syntaxhighlight lang="java">
 +
public class ListNode {
 +
public int val;
 +
public ListNode next = null;
 +
 +
public ListNode(int val) {
 +
this.val = val;
 +
this.next = null;
 +
}
 +
 +
public ListNode() {
 +
this(0);
 +
}
 +
}
 +
</syntaxhighlight>
 +
 +
=== Main.java ===
 +
<syntaxhighlight lang="java">
 +
import java.io.BufferedReader;
 +
import java.io.IOException;
 +
import java.io.InputStreamReader;
 +
 +
public class Main {
 +
private int n,m;
 +
private int[] c;
 +
private int[] d;
 +
private int[] p;
 +
 +
public static void main(String[] args) throws IOException {
 +
Main main = new Main();
 +
 +
main.process();
 +
}
 +
 +
private void process() throws IOException {
 +
    readInput();
 +
   
 +
    ListNode[] nodes = new ListNode[n + m + 1];
 +
   
 +
    ListNode head = null;
 +
    ListNode tail = null;
 +
   
 +
    for(int i=0; i<n; i++) {
 +
    ListNode newNode = new ListNode(i+1);
 +
    nodes[i+1] = newNode;
 +
   
 +
    if(tail != null) {
 +
    tail.next = newNode;
 +
    tail = newNode;
 +
    } else {
 +
    head = tail = newNode;
 +
    }
 +
    }
 +
   
 +
    for(int i=0; i<m; i++) {
 +
    int ballNumber = n + i + 1;
 +
    int pred = p[i];
 +
   
 +
    ListNode newNode = new ListNode(ballNumber);
 +
    nodes[ballNumber] = newNode;
 +
   
 +
    /* -----  this is the deleted code where we look for the node with val=pred
 +
    ListNode currentNode = head;
 +
    while(currentNode != null) {
 +
    if(currentNode.val == pred) {
 +
    break;
 +
    }
 +
    currentNode = currentNode.next;
 +
    }
 +
    */
 +
   
 +
    ListNode currentNode = nodes[pred];
 +
   
 +
    newNode.next = currentNode.next;
 +
    currentNode.next = newNode;
 +
    }
 +
   
 +
    ListNode currentNode = head;
 +
    while(currentNode != null) {
 +
    System.out.println(currentNode.val);
 +
    currentNode = currentNode.next;
 +
    }
 +
}
 +
 +
private void readInput() throws IOException {
 +
    BufferedReader reader = new BufferedReader(
 +
              new InputStreamReader(System.in) );
 +
 +
    String[] items = reader.readLine().split(" ");
 +
n = Integer.parseInt(items[0]);
 +
m = Integer.parseInt(items[1]);
 +
 +
c = new int[n];
 +
d = new int[m];
 +
p = new int[m];
 +
 +
for(int i=0; i<n; i++) {
 +
c[i] = Integer.parseInt(reader.readLine());
 +
}
 +
 +
for(int j=0; j<m; j++) {
 +
items = reader.readLine().split(" ");
 +
d[j] = Integer.parseInt(items[0]);
 +
p[j] = Integer.parseInt(items[1]);
 +
}
 +
}
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

รุ่นแก้ไขเมื่อ 07:00, 25 สิงหาคม 2559

Back to 01204212

First implemenation with arrays

FlexiArray.java

public class FlexiArray<T> {

	public interface ItemMatcher<T> {
		boolean isMatch(T item); 
	}
	
	private T[] items;
	private int itemCount;
	private int size;

	@SuppressWarnings("unchecked")
	public FlexiArray(int size) {
		this.items = (T[]) new Object[size];
		this.size = size;
		this.itemCount = 0;
	}
	
	public int findIndex(ItemMatcher<T> matcher) {
		for(int i=0; i<itemCount; i++) {
			if(matcher.isMatch(items[i])) {
				return i;
			}
		}
		return -1;
	}
	
	public void insert(int i, T item) {
		for(int j=itemCount - 1; j >= i; j--) {
			items[j + 1] = items[j];
		}
		items[i] = item;
		itemCount++;
	}

	public void remove(int i) {
		for(int j = i + 1; j < itemCount; j++) {
			items[j - 1] = items[j];
		}
		itemCount--;
	}
	
	public T get(int i) {
		return items[i];
	}
}

Examples of lambda expression

Using lambda expression:

			pLocation = outComments.findIndex((Comment c) -> (c.getId() == p));

Without lambda (anonymous class):

			pLocation = outComments.findIndex(new FlexiArray.ItemMatcher<Comment>() {
				public boolean isMatch(Comment c) {
					return c.getId() == p;
				}
			});

Zooma 1 solution with linked list (ListNode)

ListNode.java

public class ListNode {
	public int val;
	public ListNode next = null;
	
	public ListNode(int val) {
		this.val = val;
		this.next = null;
	}
	
	public ListNode() {
		this(0);
	}
}

Main.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
	private int n,m;
	private int[] c;
	private int[] d;
	private int[] p;
 
	public static void main(String[] args) throws IOException {
		Main main = new Main();
 
		main.process();
	}
 
	private void process() throws IOException {
	    readInput();
	    
	    ListNode[] nodes = new ListNode[n + m + 1];
	    
	    ListNode head = null;
	    ListNode tail = null;
	    
	    for(int i=0; i<n; i++) {
	    	ListNode newNode = new ListNode(i+1);
	    	nodes[i+1] = newNode; 
	    	
	    	if(tail != null) {
	    		tail.next = newNode;
	    		tail = newNode;
	    	} else {
	    		head = tail = newNode;
	    	}
	    }
	    
	    for(int i=0; i<m; i++) {
	    	int ballNumber = n + i + 1;
	    	int pred = p[i];
	    	
	    	ListNode newNode = new ListNode(ballNumber);
	    	nodes[ballNumber] = newNode;
	    	
	    	/* -----  this is the deleted code where we look for the node with val=pred
		    ListNode currentNode = head;
		    while(currentNode != null) {
		    	if(currentNode.val == pred) {
		    		break;
		    	}
		    	currentNode = currentNode.next;
		    }
	    	*/
	    	
	    	ListNode currentNode = nodes[pred];
	    	
		    newNode.next = currentNode.next;
		    currentNode.next = newNode;
	    }
	    
	    ListNode currentNode = head;
	    while(currentNode != null) {
	    	System.out.println(currentNode.val);
	    	currentNode = currentNode.next;
	    }
	}
 
	private void readInput() throws IOException {
	    BufferedReader reader = new BufferedReader(
	               new InputStreamReader(System.in) );
 
	    String[] items = reader.readLine().split(" "); 
		n = Integer.parseInt(items[0]);
		m = Integer.parseInt(items[1]);
 
		c = new int[n];
		d = new int[m];
		p = new int[m];
 
		for(int i=0; i<n; i++) {
			c[i] = Integer.parseInt(reader.readLine());
		}
 
		for(int j=0; j<m; j++) {
			items = reader.readLine().split(" ");
			d[j] = Integer.parseInt(items[0]);
			p[j] = Integer.parseInt(items[1]);
		}
	}
}