搜尋此網誌

2012年3月29日 星期四

Html-margin , border, padding

margin: 元件本身和其他元件之間的空間
border: 填滿元件本身的邊界
padding: 填滿元件內容到元件邊界之間的空間.

2012年3月27日 星期二

CSS line-height usage

This property can align the text within a input tag






style='left: 210; top: 22; width: 110; line-height: 54px; height: 54; font-size: 36;' />




2012年3月26日 星期一

Java Static Variable


public class StaticVariableTest {

public static void main(String []args){
Student s1 = new Student();
Student.height =1;
Student s2 = new Student();
Student.height =2;
System.out.println(Student.height);
}

}

class Student{
static int height;
}

Java ArrayList


public class ArrayListDemo {

public static void main(String[] args) {
/**
* Following example will cause a run time exception:
* java.lang.ClassCastException, although no compilation error happened.
*/

/*
* int sum = 0;
* ArrayList al = new ArrayList();
* al.add("1");
* al.add(new Integer(2));
* for (int i = 0; i < al.size(); i++) {
* sum+= (Integer)al.get(i);
* }
* System.out.println(sum);
*/

/**
* Following example will fix above problem.
*/
int sum = 0;
ArrayList al = new ArrayList();
// al.add("1"); this line has a compilation error.
al.add(new Integer(1));
al.add(new Integer(2));
for (int i = 0; i < al.size(); i++) {
sum += (Integer) al.get(i);
}
System.out.println(sum);
}

}

2012年3月25日 星期日

Merge Sort (合併排序法)


package sort;

public class MergeSortDemo {


public static void main(String[] args) {
int[] array = { 3, 3,2,1,0,4,5,1,4};
array = mergeSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}

public static int[] mergeSort(int[] list) {
//if merely left 1 element return list;
if (list.length <= 1) {
return list;
} else {
int[] left;
int[] right;
int middle = list.length / 2;
//declare left sub array;
left = new int[middle];
//declare right sub array;
right = new int[list.length - middle];
int j = 0;
for (int i = 0; i < list.length; i++) {
if (i < left.length) {
//initialize left array
left[i] = list[i];
} else {
//initialize right array
right[j] = list[i];
j++;
}
}
//divide left array into small parts;
left = mergeSort(left);
//divide right array into small parts;
right = mergeSort(right);
//combine small parts of left and right
return merge(left, right);
}
}

public static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int j = 0;
while (left.length > 0 || right.length > 0) {
//left and right array have elements.
if (left.length > 0 && right.length > 0) {
if (left[0] <= right[0]) {
result[j] = left[0];
left = shiftArray(left);
} else {
result[j] = right[0];
right = shiftArray(right);
}

} else if (left.length > 0) {
//left array has elements;
result[j] = left[0];
left = shiftArray(left);
} else if (right.length > 0) {
//right array has elements;
result[j] = right[0];
right = shiftArray(right);
}
j++;
}
return result;
}

private static int[] shiftArray(int[] i) {
int[] result = new int[i.length - 1];
for (int j = 0; j < result.length; j++) {
result[j] = i[j+1];
}
return result;
}
}




Reference Wiki

2012年3月24日 星期六

Recursive Selection Sort (遞迴選擇排序法)

維護舊有Java程式專案
1:  public class RecursiveSelectionSort {  
2:      public static void main(String[] args) {  
3:          int [] array ={5,2,3,6,1,7,0,9,100,4,8};  
4:          selectionSort(array, array.length);  
5:          for(int i=0;i<array.length;i++){  
6:              System.out.print(array[i]+" ");  
7:          }  
8:      }  
9:      /**  
10:       * Using the concept of selection sort.  
11:       * Implemented by an iterative and a recursive mechanisms.  
12:       *   
13:       * Following are steps:  
14:       * 1.Finding the smallest one and its index;  
15:       * 2.Put the smallest into the right end side;  
16:       * 3.Decrease the array size;  
17:       * Recursively calling this method.  
18:       *   
19:       * @param array unsorted array  
20:       * @param length array length  
21:       */  
22:      public static void selectionSort(int[] array , int length) {  
23:          if(length > 0){  
24:              int smallestIdx = 0 ;  
25:              int smallest = array[smallestIdx];  
26:              //1.From left to right find the smallest one and its index;  
27:              for(int i =0 ;i<length ;i++){  
28:                  if(array[i]< smallest){  
29:                      smallest = array[i];  
30:                      smallestIdx = i;  
31:                  }  
32:              }  
33:              //2.Put the smallest into the right end side;  
34:              swapElements(array,smallestIdx,length -1);  
35:              //3.Decrease the array size;  
36:              length -=1;  
37:              //4.Recursively calling this method.  
38:              selectionSort(array,length);  
39:          }  
40:      }  
41:      private static void swapElements(int[] array, int idx, int idx1) {  
42:          int tmp = array[idx];  
43:          array[idx] = array[idx1];  
44:          array[idx1] = tmp;  
45:      }  
46:  }  

一中兩區

一中兩區?? 統一不是壞事, 如果跟民主國家統一那沒問題,

要跟共產國家統一??請舉辦公投,如果通過的話,就統一好了,

再這樣下去,會真的成為馬區長.!!

最好是維持現狀,等待中國成為民主國家再說

2012年3月23日 星期五

Binary Search (Recursive version)

Before you do binary search, you should sort the unsorted elements first 在使用binary搜尋之前, 要先把資料排序過. Sort(排序)

public class BinarySearch {
 
 public static void main(String [] args){
  int [] i ={2,3,4,4,5,6,7,87};
  System.out.println(binarySearch(i,0,i.length-1,5));
  
 }
 
 public static int binarySearch(int[] i, int left, int right, int target) {
  int middle = (left+right)/2;
  if (i[middle] == target) {
   return middle;
  } else if (i[middle] > target) {
   return binarySearch(i,left,middle,target);
  } else{
   return binarySearch(i,middle,right,target);
  }
 }
}



A c++ binary search tree

Iterative and Recursive

2012年3月17日 星期六

Java Inner Class(內部類別)


public class InnerClassDemo {

class Student{
void print(){
System.out.println("This is an inner class as an instance variable");
}
}

void classInAmethod(){
class Son{
void print(){
System.out.println("I am within a method");
}
}
Son s = new Son();
s.print();
}

public static void main(String[]args){
InnerClassDemo is = new InnerClassDemo();
is.classInAmethod();
InnerClassDemo.Student s = is.new Student();
s.print();
}

}


資料來源:http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
相關文章:anonymous class

你真的是一個programmer嗎??

光會if-else, for or while loop, call 3rd party library, or 知道常用的framework, 對於programmer 來說, 以上只是必須的基本技巧, 嚴格來說要能實做出各種演算法,才能說自己會寫程式.

Java 變數指定


import java.util.ArrayList;

public class VariableAssignDemo {

public static void main(String[] args){
ArrayList a1 = new ArrayList();
a1.add(1);
/**
* Following case assigns object reference to a new variable
*/
ArrayList a2 =a1;
a2.add(2);
System.out.println(a1.toString());
System.out.println(a2.toString());

System.out.println("-------------------");

/**
* Following is a deep copy , all elements are included..
* If you want to keep original object , but do some changes on it.
* A deep copy is a solution.
*/
ArrayList a4 =new ArrayList();
for(int i = 0 ;i < a1.size() ;i++){
a4.add(a1.get(i));
}
a1.remove(0);
System.out.println(a1.toString());
System.out.println(a4.toString());

}
}

Java method override


public class OverwriteDemo {

}

class Father{

void print(){

}
}

class Son extends Father{

// compile-error
// private void print(){
// }

protected void print(){

}
}

2012年3月12日 星期一

Java Properties


 import java.io.*;  
import java.util.Properties;
public class PropertiesDemo {
/**
* File Name: some.prop
* Content : msg= Hello World.
*/
public static void main(String[] args) throws IOException {
// First you must create a file named "some.prop" then choose it .
FileInputStream fis = new FileInputStream("some.prop");
Properties prop = new Properties();
prop.load(fis); // Load properties from file input stream
String meesage = (String) prop.get("msg"); // get entry key
System.out.println(meesage);// print it out
prop.setProperty("msg", "modified Hello World");// add or modify new properties.
FileOutputStream fos = new FileOutputStream("some.prop");// Get property source file again.
prop.store(fos, "modified propery file");// Save the newest contents into the property file
prop.list(System.out);// List them out.
}
}


相關連結:
http://goodideascome.blogspot.com/2011/12/spring-ioc-with-hello-world-example.html

2012年3月11日 星期日

Java exception -例外處理

Runtime exception: 非程式設計師所能預期到的錯誤.可直接拋出.

如果類似檔案找不到 ,這種可預期的錯誤狀況便要利用

try ..catch 把錯誤接收起來並做適當的處理

import java.io.*; 
public class ExceptionDemo {
public static void main(String[] args) throws FileNotFoundException{
int i =Integer.parseInt("r"); // this is a runtime exception
System.out.println(i);
//using a try catch block to catch checked exception
try {
FileReader fr = new FileReader(new File("xxx.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//or throw out this exception to the caller.
FileReader fr = new FileReader(new File("xxx.txt"));
}
}



Following is a fail case.



public class UnreachableDemo {
public static void main(String[] args){
int [] k =new int []{1,2,3};
for(int i = 0 ; i < k.length+1 ; i++ ){
System.out.println(k[i]);
}
System.out.println("arrive here?");
}
}




Following is a success case.




public class ReachableDemo {
public static void main(String[] args){
int [] k =new int []{1,2,3};
try{
for(int i = 0 ; i < k.length+1 ; i++ ){
System.out.println(k[i]);
}
System.out.println("within a try block , so it is unreachable");
}catch(Exception e){
e.printStackTrace();
}
System.out.println("outside");
}
}

Java action listener

Inner Class implementation:



Change getHelloButton with follwing example, you will get an anonymous version implementation:

2012年3月10日 星期六

Java 物件比較

因為底下這兩個物件都有相同的記憶體位址,所以都是true.

如果equals方法,有被覆寫過的話,那麼hashCode方法也需要被覆寫, 以確保物件的相同.


Java 物件equals method 的用法

什麼是unicode?

unicode是一種字元編碼的方式,
每一個字元無論在哪一種作業系統平台,或程式語言都只有一個唯一utf的號碼.

下面這個例子是寫入中文, 然後用英文編碼讀取, 所以會讀出亂碼.


若寫入和讀出都是使用unicode的話,就可以正常顯示.

JSP 標籤函式庫

uri :此函式庫的位址.
prefix : 如果有引用很多tag library的話,可以用來區別各個不同的函式庫的名稱.

SCWCD 考試目標 :JSP技術-1

被包覆在out.println()可列印出來的部分,還有網頁本身的文字都算是template text.



scripting elements (comments, directives, declarations, scriptlets, and expressions)

comments: 註解


directives: 指令,jsp有三種指令: page , include , taglib


declarations: 變數宣告


scriptlet: 一般Java的程式碼


expressions: 描述

2012年3月9日 星期五

2012年3月8日 星期四

Java thread(執行緒)

Java 物件轉型

Animal ani = new Dog();
隱性轉型,此時的ani變數只可使用Animal類別提供的方法,
無法使用Dog類別的方法.
(可以大容器裝小物件)

Dog dg1 = new Animal();
(小容器裝大物件,會產生編譯錯誤)

Dog dg2 = (Dog)ani;
明確轉型,此時我們指定animal就是dog.
這是多型的用法,假如有一個cat類別也繼承animal,
也可轉型為cat來使用.

如果不確定執行時期的物件是哪一個類別的實體,
可以使用instanceOf這個方法來判斷,以避免丟出

Java 字元編碼轉換

native2ascii 欲轉換的中文字

會印出unicode版本的資源編碼.


2012年3月4日 星期日

Java Serialization (Java 序列化)

一般來說物件實體只能存活在JVM中如果想要把物件的實體狀態給保留下來的話,可以使用序列化這個技術,以下是把物件實體寫入檔案的例子.





而SerializationReadSample.java則是把剛剛序列化後的物件,讀回來的程式碼:


只要欲保存的類別有implement Serializable這個界面, 就可以拿來作序列化的動作.

當類別中有不想被序列化的field, 例如,socket, or thread等等的動態內容,

則可以在宣告變數的時候加上transient 這個關鍵字.


千萬不可序列化內部類別(inner class) , 無論是local或是匿名內部類別. 因為這麼做的話 ,

compiler會為內部類別產生合成的建構子.  因為合成的建構子 , 在source code找不到實作的細節

會隨著java compiler版本的不同, 而產生不同的建構子, 也就是說你在A版本編譯出來的code,

很可能在B版本跑不起來 , 造成相容性的問題 .

2012年3月1日 星期四

容錯(Fail-over support)和負載平衡(Load Balancing)

容錯: 當一台server無法服務客戶端的請求時, 其他的server可以立刻把所有的服務請求接收過來執行.

負載平衡: server會把客戶端請求,分配給目前工作loading較不重的server來執行.