標籤

二元樹 (1) 字串常數池 (1) 投資 (3) 每月損益 (37) 例外處理 (1) 泛型 (2) 股票 (15) 指標 (5) 英文 (8) 基本資料型別 (1) 期貨 (1) 程式交易 (10) 量化投資 (5) 亂亂寫 (3) 概念 (3) 資料結構 (3) 演算法 (3) 數學 (3) 轉型 (1) AMA (1) ArrayList (1) assert (1) BeautifulSoup (1) C/C++ (8) casting (1) ClassCastException (1) classpath (1) Collection (4) Comparable (1) comparTo() (1) constructor (1) database (3) Debian (1) Dropbox (2) EA (2) enum (1) equals() (2) exception (3) extends (1) ffmpeg (1) final (1) Git (1) HashMap (1) HashSet (1) hasNext() (1) HTS (3) instanceof (1) IS-A (1) Iterator (1) JAVA (43) length (1) Linux (31) List (1) Mac (6) Map (1) ML (2) MT4 (6) MySQL (2) next() (1) NullPointerException (1) Number (1) Numpy (2) OpenCart (1) OpenCV (3) OSX (1) overloading (1) overriding (3) pandas (2) PHP (8) PriorityQueue (1) Python (11) Queue (1) random() (1) reverse() (1) Samba (1) SCJP (21) sqrt() (1) synchronized (1) talib (1) ufw (1) uTorrent (1) var-args (2) VHF (1) vim (2) Yhoo知識+ (4)
顯示具有 JAVA 標籤的文章。 顯示所有文章
顯示具有 JAVA 標籤的文章。 顯示所有文章

2013年10月7日 星期一

安裝Java7 JDK在Ubuntu上

There is a similar question on how to install JRE 7.

Install the JDK

  • Download he 32bit or 64bit Linux "compressed binary file" - it has a ".tar.gz" file extension i.e. "[java-version]-i586.tar.gz" for 32bit and "[java-version]-x64.tar.gz" for 64bit
  • Uncompress it
    tar -xvf jdk-7u2-linux-i586.tar.gz (32bit)
    tar -xvf jdk-7u2-linux-x64.tar.gz (64bit)
JDK 7 package is extracted into ./jdk.1.7.0_02 directory. N.B. check carefully this folder name since Oracle seem to change this occasionally with each update.
  • Now move the JDK 7 directory to /usr/lib
sudo mkdir -p /usr/lib/jvm
sudo mv ./jdk.1.7.0_02 /usr/lib/jvm/jdk1.7.0
  • Now run
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1
  • This will assign Oracle JDK a priority of 1, which means that installing other JDKs will replace it as the default. Be sure to use a higher priority if you want Oracle JDK to remain the default.
  • Correct the file ownership and the permissions of the executables:
sudo chmod a+x /usr/bin/java 
sudo chmod a+x /usr/bin/javac 
sudo chmod a+x /usr/bin/javaws
sudo chown -R root:root /usr/lib/jvm/jdk1.7.0
N.B. remember - Java JDK has many more executables that you can similarly install as above. java, javac, javaws are probably the most frequently required.
This answer lists the other executables available.
  • Run
sudo update-alternatives --config java
You will see output similar one below - choose the number of jdk1.7.0 - for example 3 in this list:
$sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
————————————————————
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode
1 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
3 /usr/lib/jvm/jdk1.7.0/jre/bin/java 3 manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/lib/jvm/jdk1.7.0/jre/bin/java to provide /usr/bin/java (java) in manual mode.
Check the version of you new JDK 7 installation:
java -version
java version “1.7.0”
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode) 
Repeat the above for:
sudo update-alternatives --config javac
sudo update-alternatives --config javaws
 
參考自: http://askubuntu.com/questions/55848/how-do-i-install-oracle-java-jdk-7

2013年10月6日 星期日

Lubuntu安裝Java7記錄

去下載jdk7之後,解壓縮在自己喜歡的資料夾,然後# java -version ,會看不到java版本。所以:
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java7-installer
之後,就看的到java -version了。

2012年12月7日 星期五

三維陣列走訪

試撰寫一程式,請利用三維列陣A完成下列題目的要求。列陣A的值如下
int A[][][]={{{15,50,65},{38,94,25},
{79,44,19},{89,54,73}},
    {{14,90,46},{43,23,67},
{32,56,78},{94,78,40}}};

(a)請印出陣列內容。
(b)在陣列A裡找出所有大於等於50的元素,將該元素重新設值為99。
(c)印出完成(b)之後的陣列內容。


--------------------------------------

public class Array99{
public static void main(String[] args){
int[][][] A={{{15,50,65},{38,94,25},{79,44,19},{89,54,73}},
    {{14,90,46},{43,23,67},{32,56,78},{94,78,40}}};
    showArray(A);
    showArray(A);
}
public static void showArray(int[][][] a){
for(int i=0; i<a.length; i++){
for(int j=0; j<a[0].length; j++){
for(int k=0; k<a[0][0].length; k++){
System.out.print(a[i][j][k]+ " ");
if(a[i][j][k]>=50) a[i][j][k] = 99;
}
System.out.println();
}
}
System.out.println();
}
}

補充:其實for迴圈那裡用for-each會比較好理解,如for(int[][] b: a)...for(int[] c: b)...for(int d: c),不過這樣就不能指派值了。

好啦,還是補一下for-each的版本:

public class Array99{
public static void main(String[] args){
int[][][] A={{{15,50,65},{38,94,25},{79,44,19},{89,54,73}},
    {{14,90,46},{43,23,67},{32,56,78},{94,78,40}}};
    showArray(A);
    showArray(A);
}
public static void showArray(int[][][] a){
for(int[][] i: a){
for(int[] j: i){
for(int k=0; k<j.length; k++){     //這裡如果用for(int k: j)會無法指派值
System.out.print(j[k]+ " ");
if(j[k]>=50) j[k] = 99;
}
System.out.println();
}
}
System.out.println();
}
}

Euler的圓周率


public class Pi{
public static void main(String[] args){
System.out.println("Euler(10)=" +Euler(10));
System.out.println("Euler(100)=" +Euler(100));
System.out.println("Euler(1000)=" +Euler(1000));
}
public static double Euler(int n){
boolean isPrime;
double sum = 1.0;
int count = 0;
int i = 3;
while(count < n){
isPrime = true;
for(int j=3; j<=Math.sqrt(i); j+=2){
if(i%j == 0){
isPrime = false; break;
}
}
if(isPrime){
if((i-1)%4 == 0) sum *= i / (double)(i+1);
else sum *= i / (double)(i-1);
count++;
}
i += 2;
}
return sum * 2;
}
}

數字反轉


請利用eclipse打一支程式
題目如下:
實作一個public static int reverse(int n)方法, 將一個5位數的數字逆轉. EX: 呼叫 reverse(12345) 會得到一個 54321 的結果. (提示: 可利用求餘數的方法將五個數字分別切割出來, 然後再逆轉組合. 12345 % 10 = 5)

PS:用method做

-----------------------------------

public class ReverseNum{
public static int reverse(int n){
String wn = "";
wn += n;
StringBuilder sbd = new StringBuilder(wn);
sbd.reverse();
return Integer.parseInt(sbd.toString());
}
public static void main(String[] args){
System.out.println(reverse(12345));
}
}


Java的樂透問題


拜託在後面寫一下解釋~謝謝
1.
請設計一個程式會進行樂透的開獎&對獎的過程。也就是程式會要求使用者輸入6個1~49的相異整數,代表欲對獎的彩券號碼。然後產生6個相異整數,代表某期大樂透開獎的號碼。接著程式會對獎之,並分別輸出開獎號碼、彩券號碼(均小至大排列)及中了幾個號碼!如:
請輸入你的彩券第1個號碼(1~49)
32
請輸入你的彩券第2個號碼(1~49)
4
請輸入你的彩券第3個號碼(1~49)
31
請輸入你的彩券第4個號碼(1~49)
16
請輸入你的彩券第5個號碼(1~49)
28
請輸入你的彩券第6個號碼(1~49)
2
本期開出的號碼(由小至大排列)是:4,7,16,28,33,49
你的彩券的號碼(由小至大排列)是:2,4,16,28,31,32
你中了3個號碼

----------------------------------------------

import java.util.*;
public class BigMoney{
public static final int GUESS = 6;
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int[] input = new int[GUESS];
int[] number = new int[GUESS];
int get = 0;
makeNum(number); //產生樂透號碼

for(int i=0; i<input.length; i++){
System.out.print("請輸入你的彩券第" +i+ "個號碼(1~49):");
input[i] = Integer.parseInt(sc.nextLine()); //將輸入轉成整數存入陣列
for(int j=i; j>0; j--){
if(input[i] == input[j-1] || input[i] > 49){
System.out.println("輸入重複或超出範圍");
i--;
break;
}
}
}

Arrays.sort(number); //陣列做排序
System.out.print("本期開出的號碼(由小至大排列)是:");
for(int i: number) System.out.print(i+ " ");

Arrays.sort(input);
System.out.print("\n你的彩券的號碼(由小至大排列)是:");
for(int i: input){
for(int j: number) if(i == j) get++; //計算有中的號碼
System.out.print(i+ " ");
}
System.out.println("\n你中了" +get+ "個號碼");
}

public static void makeNum(int[] n){
for(int i=0; i<n.length; i++){
n[i] = (int)(Math.random()*49+1);
for(int j=i; j>0; j--){
if(n[i] == n[j-1]){
i--;
break;
}
}
}
}
}

2012年11月10日 星期六

覆寫函式的可控式例外

覆寫規則其中一條是:不能宣告範圍更大或新的exception。
2012.11.18補充:除非新的exception為RuntimeException或其子類別。

例1:

class A{
    A length(){return this;}
}
class Test1 extends A{
        Test1 length()throws NullPointerException{ return this;}

        public static void main(String[] args){
                System.out.println(new Test1().length());
        }
}
------------
Test1@57f0dc

說明:
Test1 length()throws NullPointerException{ return this;}:劃線為合法,因為可宣告回傳型別為父函式的子型別;又因為父函式未宣告exception,所以表示父函式可能丟出任何exception,而NullPointerException為exception的子類別,符合覆寫規則。
證明:
class A{A length()throws NullPointerException{return this;}}
class Test1 extends A{
        Test1 length()throws Exception {return this;}
        public static void main(String[] args){
                System.out.println(new Test1().length());
        }
}
---------------
Test1.java:4: length() in Test1 cannot override length() in A; overridden method does not throw java.lang.Exception
Test1 length()throws Exception {return this;}
              ^

例2:
class MyException extends Exception{}
class A{A length(){return this;}}
class Test1 extends A{
        Test1 length()throws MyException {return this;}
        public static void main(String[] args){
                System.out.println(new Test1().length());
        }
}
----------------
Test1.java:4: length() in Test1 cannot override length() in A; overridden method does not throw MyException
Test1 length()throws MyException {return this;}
              ^
不能丟新的exception。不過明明MyException就有繼承Exception啊,為何還是無法通過編譯呢?

2012.11.18更新
上面觀念錯了,在此重新𨤳淸:
一個覆寫的函式的確不能丟出範圍比父函式要大的例外。
只有一種可以:RuntimeException(包含其子類別)可以由子函式丟出。因為RuntimeException為非可控式例外(unchecked exception)不用去處理;基本上無視它就是了。
所以上面例1的Test1 length()throws NullPointerException{ return this;},NullPointerException為RuntimeException的子類別,所以子函式雖然有宣告,但編譯器無視它,當做沒這回事,當然就能順利編譯。但例2的Test1 length()throws MyException {return this;},MyException為Exception的子類別,非屬於RuntimeException,所以編譯無法通過。

2012年11月6日 星期二

基本資料的型別不同可以比較耶


public class Test1{
        public static void main(String[] args){
                float f = 1f;
                long lo = 1;
                int in = 1;
                double d = 1.0;
                if(f == lo)     System.out.println("1.0==1");
                if(in == d)     System.out.println("1==1.0");
        }
}

------------
1.0==1
1==1.0

應該是做比較的時候會將小型別自動轉型成比較大的那個型別吧。

2012年11月5日 星期一

關於final

1、final類別不能被繼承。
2、final函式不能被覆寫(可繼承)。
3、final變數一定要給初始值,且不能被修改。

2012年11月1日 星期四

關於轉型

轉型是轉參考變數的型別,不是轉物件。

2012年10月29日 星期一

var-args參數

函式的var-args參數宣告要放在後面。

void count(int... x)(){}    //OK!
void count(int x, int... y)(){}    //OK!
void count(int... x, int y)(){}    //NO!!!不能放在前面!

instanceof

if(物件o instanceof 類別A),若物件能通過IS-A類別的測試,回傳true。
所以不管物件o的型別是什麼,看物件就對了。


interface Foo{ }
class Alpha implements Foo{ }
class Beta extends Alpha{ }
public class Delta extends Beta{
        public static void main(String[] args){
                Beta x = new Beta();
                Foo f = (Delta)x;    //這行註解起來可順利執行
                Foo ff = (Alpha)x;
                Beta b = (Beta)(Alpha)x;    //這行能過
        }
}


上面程式碼可順利編譯(因為…我也不知道),但執行只有第7行會出現下面的例外

Exception in thread "main" java.lang.ClassCastException: Beta
at Delta.main(Delta.java:7)

 Beta b = (Beta)(Alpha)x; 
//這行能過是因為x指向的物件是Beta,用instanceof來測試,x is-a Alpha,x is-a Beta,都可以通過IS-A的測試。

Foo f = (Delta)x; 
不能通過的原因是,x is-a Delta,不能通過IS-A測試。

只有try可以嗎?

不可以,會編譯錯誤。有try至少要緊跟著catch或finally其中之一;若兩者都有,finally要跟在catch後面。

try{
    //可能會產生例外的程式碼
}catch(exception e){
    //例外處理
}finally{
    //這裡的程式碼一定會運行
}

length跟length()

String是用length()方法,陣列用length屬性;來返回物件長度。
有點難記…字串方法,陣列屬性嗎?

若參考變數都指向NULL,雖可通過編譯,但執行都會丟出NullPointerException例外。


public class lengthTest{
        public static void main(String[] args){
                String str = null;
                System.out.println(args.length);
                System.out.println(str.length());    //丟出例外
        }
}




0
Exception in thread "main" java.lang.NullPointerException
at lengthTest.main(lengthTest.java:5)

2012年10月28日 星期日

comparTo()

comparTo()為Comparable介面的函式,要實現排序須實作comparTo()函式。
當呼叫Collection.sort()時,系統會依comparTo()的回傳值由小到大做排序。

a, b為物件。
int x = a.comparTo(b);    //由小到大
if a < b, then x < 0
if a = b, then x = 0
if a > b, then x > 0
如果a < b,則x < 0,a.comparTo(b)排序為a b

若
int x = b.comparTo(a);
if b < a, then x < 0
if b = a, then x = 0
if b > a, then x > 0
如果a < b,則x > 0,b.comparTo(a) 排序為b a

結論:
a.comparTo(b)與b.comparTo(a)的排序結果相反。
記的方法為,a.comparTo(b)就是a - b,若a小減的結果為負,a就放前面;
b.comparTo(a)就是b - a,若a小減的結果為正,a放後面;
兩者剛好相反。



Number抽象類別

The abstract class Number is the superclassof classes BigDecimal, BigInteger, Byte, Short, Integer, Float, Long and Double.
Subclasses of Number must provide methods to convert the represented numeric value to byte, double, float, int, long, and short.

2012年10月27日 星期六

PriorityQueue的函式

PriorityQueue實作Queue介面,Queue介面繼承Collection介面。
PriorityQueue預設排序為自然排序,或是自定義Comparator。

PriorityQueue<String> pq = new PriorityQueue<String>();
pq.add("carrot");
pq.add("apple");
pq.add("banana");       //pq裡元素順序為apple banana carrot
System.out.println(pq.poll()+ ":" +pq.peek());

======
apple:banana


poll():回傳最高優先權元素後予以刪除
peek():回傳最高優先權元素

HashMap宣告

HashMap實作Map介面,Map內為key/value兩個物件的組合,key為唯一值。
HashMap無順序無排序。
因key值為唯一值,必須覆寫equals()判斷物件key,並覆寫hashCode()。

*一旦覆寫equals()同時也必須覆寫hashCode(),因為equals()回傳ture表示兩物件(意義)相同時,因兩物件在記憶體裡位址不同,所以此時hashCode值必定不同,因此必須覆寫hashCode()讓回傳值一致。

HashMap<String, Integer> hmap = new HashMap<String, Integer>();
HashMap<key值, value值>

重要函式有:
put(Object key, Object value)    //將key跟value加入HashMap    ex: hmap.put("abcdef", 1);
keySet()    //以Set回傳key值    ex: Set<String> ss = hmap.keySet();

void addAnimal(List<? extends Animal> animals){}

?為萬用字元,表示任何的意思。
<? extends Animal>的意思是說,任何繼承Animal的子型別。
<? super Animal>的意思是說,任何高於Animal的型別。

?不能用在類別泛型宣告上:
class A<? extends Animal>{ }    //打妹!
class A<Dog extends Animal>{ }    //OK!

取得Iterator的方式

有實作Collection介面就會能取得Iterator:

List<Integer> intList = new ArrayList<Integer>();
Iterator<Integer> ite = intList.iterator();    //返回intList的Iterator

就能用Iterator的函式了:
ite.hasNext();    //看看還有沒有下一個
ite.next();          //丟出後向前跳