public ArrayList<Integer> grayCode(int n) {
		ArrayList<Integer> result = new ArrayList<Integer>();

		if (n == 0)
			return result;

		// form two arrays, digitArray is digit from lower position to higher
		// position, so digitsArray[0] meaning 0/1 on the lowest position of n,
		// valueArray is decimal value represented corresponding to the position
		// in digitsArray, so valueArray[0] would be 2 power 0 = 1;
		int[] digitsArray = new int[n];
		int[] valueArray = new int[n];
		int[] toDivArray = new int[n];  // starting from 2, exponentially. This is used later for determine whether a digit should be 0 or 1 based on which number it is.
		
		for (int i = 0; i < n; i++) {
			if(i == 0 ){
				valueArray[i] = 1;
				toDivArray[i] = 2;
			} else {
				valueArray[i] = 2*valueArray[i-1];
				toDivArray[i] = 2*toDivArray[i-1];
			}
		}
		
		printIntArray(digitsArray);
		printIntArray(valueArray);
		printIntArray(toDivArray);
		
		int totalNumbers = (int)Math.pow(2, n);
		
		for(int i = 0 ;i < totalNumbers; i++){
			int curResult = 0;
			//form digitsArray according to i;
			for(int j =0 ;j < n;j++){
				int div = i/toDivArray[j]; // see how many times of 2 it is. 
				int remain = i%toDivArray[j];
				int half = toDivArray[j]/2;
				
				if (!isOddNumber(div)) { // even number, pattern would be 0,...,0,1,...1. Number of 0 would be half the size of toDivArray[i].
					if(remain < half)
						digitsArray[j] = 0;
					else
						digitsArray[j] = 1;
				}else { //pattern would be opposite to above
					if(remain < half)
						digitsArray[j] = 1;
					else
						digitsArray[j] = 0;
				}
				curResult += valueArray[j]*digitsArray[j];
			}
			//System.out.println("For i="+i);
			System.out.println("Cur result:"+curResult);
			//printIntArray(digitsArray);
			result.add(new Integer(curResult));
		}
					
		return result;
	}