2011年7月25日 星期一

Java連接Oracle教學

Java連接Oracle教學

Context configuration

In a similar manner to the mysql config above, you will need to define your Datasource in your Context. Here we define a Datasource called myoracle using the thin driver to connect as user scott, password tiger to the sid called mysid. (Note: with the thin driver this sid is not the same as the tnsname). The schema used will be the default schema for the user scott.
Use of the OCI driver should simply involve a changing thin to oci in the URL string.

<Resource name="jdbc/myoracle" auth="Container"
      type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
              username="scott" password="tiger" maxActive="20" maxIdle="10"
              maxWait="-1"/> 

web.xml configuration

You should ensure that you respect the element ordering defined by the DTD when you create you applications web.xml file.
<resource-ref>
 <description>Oracle Datasource example</description>
 <res-ref-name>jdbc/myoracle</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>





Code example

You can use the same example application as above (asuming you create the required DB instance, tables etc.) replacing the Datasource code with something like
Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();
//etc.


重點一:建議使用內建的Java 1.4.2版本,而不要去java的網站下載新版本,
      
由於目前oracleJDBC最新只有支援到1.4版本,尚未有支援1.5新版的JDBC,
      
使用1.5.0的可能會出現一些問題。

(
)依不同的版本,了解檔案所在的位置
  (1)
安裝完整版的oracle ( client + server )
    JDBC
的位置
      C:\oracle\product\10.1.0\Db_1\jdbc\lib\ojdbc14_g.jar
    Java 1.4.2
的位置
      C:\oracle\product\10.1.0\Db_1\jdk\bin\javac.exe
      C:\oracle\product\10.1.0\Db_1\jdk\bin\java.exe

  (2)
安裝client版本的oracle
    JDBC
的位置
      C:\Oracle\product\10.1.0\Client_1\jdbc\lib\ojdbc14_g.jar
    Java 1.4.2
的位置
      C:\Oracle\product\10.1.0\Client_1\jdk\bin\javac.exe
      C:\Oracle\product\10.1.0\Client_1\jdk\bin\java.exe

(
)設定classpathpath的環境變數
 
使用Windows XP作業系統者,[我的電腦]左側有[檢視系統資訊],
 
在系統內容的新視窗中,[進階]的分頁下面,有一個[環境變數],
 
按下去後,又分為上面的[xxx的使用者變數]與下面的[系統變數]

  [xxx
的使用者變數],需要設定classpath這個變數,[新增],
 
變數名稱 classpath
 
變數值 .;C:\Oracle\product\10.1.0\Client_1\jdbc\lib\ojdbc14_g.jar

  [
系統變數],需要設定Path這個變數,Path這變數上面按[編輯],
 
變數名稱 Path
 
變數值 ;C:\Oracle\product\10.1.0\Client_1\jdk\bin\

  (
以上是以client版本的路徑為例子)

(
)開始寫Java的程式
  Java
程式能連上Oracle的資料庫,主要是透過JDBC連接的,
 
JDBC就是寫在ojdbc14_g.jar這裡面(會依不同版本而不同),
 
在課程FTP站裡面,有放著助教寫的範例程式,大家可以下載來玩玩,
 
java/debug.java java/test.java java/test2.java
 
裡面還有一個 java/javadoc.zip JDBC說明文件,
 
大家可以下載解壓縮後看一看, javadoc/index.htm 裡面
 
可以看到 oracle.jdbc 這個class裡面支援的,
 
有那些 method 可以用的,都寫在裡面!!

 
重要的程式碼:

  Class.forName("oracle.jdbc.driver.OracleDriver");
  DriverManage.getConnection("jdbc:oracle:thin:@134.208.27.195:1521:ORCL"
  ,
帳號,密碼);

 
大家就開始玩玩吧!! 有不懂的地方,大家再互相討論!!

(
)常見的問題 Q&A
  (1)
確定 java -version 的版本是1.4.2
    
確定 classpath有設定好,且設定好之後,並無法馬上使用,
    
必須重新開機之後才能用!!

  (2)
若發生錯誤為
 Exception in thread "main" java.lang.NoClassDefFoundError: xxxx(
你取的名字)
    
問題是出在,你的classpath沒有設定好!!


2011年7月19日 星期二

2011-07-19 JAVA

// 求出三個人的身高、體重、年齡的最大值並顯示(使用方法)

import java.util.Scanner;


class MyMethod {
     static int max(int a, int b, int c) {
        int max = a;
        if (b > max) max = b;
        if (c > max) max = c;
        return max;
    }

}

class MaxHwaMethod_techer {

    //--- 傳回a、b、c的最大值 ---//
   

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int[] height = new int[3];        // 身高
        int[] weight = new int[3];        // 體重
        int[] age    = new int[3];        // 年齡

        for (int i = 0; i < 3; i++) {    // 讀入
            System.out.print("[" + (i + 1) + "] ");
            System.out.print("身高:");        height[i] = stdIn.nextInt();
            System.out.print("    體重:");    weight[i] = stdIn.nextInt();
            System.out.print("    年齡:");    age[i]    = stdIn.nextInt();
        }

        int maxHeight = MyMethod.max(height[0], height[1], height[2]);    // 身高的最大值
        int maxWeight = MyMethod.max(weight[0], weight[1], weight[2]);    // 體重的最大值
        int maxAge    = MyMethod.max(age[0], age[1], age[2]);        // 年齡的最大值

        System.out.println("身高的最大值為" + maxHeight + "。");
        System.out.println("體重的最大值為" + maxWeight + "。");
        System.out.println("年齡的最大值為" + maxAge    + "。");
    }
}
----------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;

class MyP7_3 {

  
    static int med(int a, int b, int c) {
        int med = 0;
        if (a >=b && a<=c || (a>b && a<c))
            med = a;
        if (b>=a && b<=c || (b>a && b<c))
            med = b;
        if (c>=a && c<=b || (c>a && c<b))
            med = c;
        return med;
      
    }
  
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);

        int a = 2;
        int b = 4;
        int c = 6;
        System.out.println("中間值" + med(a,b,c));
    }
}
-------------------------------------------------------------------------------------------------
import java.util.Scanner;

class MyP7_3 {

  
    static int med(int a, int b, int c) {
        int med = 0;
        if (a >=b && a<=c || (a>b && a<c))
            med = a;
        if (b>=a && b<=c || (b>a && b<c))
            med = b;
        if (c>=a && c<=b || (c>a && c<b))
            med = c;
        return med;
      
    }
  
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);

        System.out.print("整數a:");  int a = stdIn.nextInt();
        System.out.print("整數b:");  int b = stdIn.nextInt();
        System.out.print("整數c:");  int c = stdIn.nextInt();
      
        System.out.println(med(a,b,c));
    }
}
-------------------------------------------------------------------------------------------------
class MyP7_66 {
   static void myArr(int[] a) {     //void不傳回值
      for (int y:a)  //加強型for
        System.out.println(y);
        //return 0;
 
   }
   public static void main(String[] args){
   int[] x={1,2,3,4,5};
   myArr(x); //x變數,copy值給a;
   //int x = myArr(x) 錯誤寫法,因為不傳值
   }

}
----------------------------------------------------------------------------------


import java.util.Random;

class MyP7_8 {

    static int random(int a, int b) {
     Random x = new Random();
        if (a>b)
           return a;
        int y = x.nextInt(b-a);
           return y+a;
      
      
    }
public static void main(String[] args) {

}

  
}
---------------------------------------------------------------------------------------------------





class MyP7_67 {

    static int sumInt(int ...x) {
     int sum = 0;
     for (int y:x) {
        sum=sum+y;
     }
        return sum;
      
    }
public static void main(String[] args) {

  System.out.println(sumInt(10,20,30));
  System.out.println(sumInt(1,2,3,4,5,6,7));
}

}


------------------------------------------------------------------------------------------
lass vlocal {
  static void z(){
  int b=0;
  System.out.println(b);
}
}
class MyP8_66 {
  public static void main(String[] args) {
   vlocal.z();
  }

}
---------------------------------------------------------------------------------------
class vlocal {
  static int a=100;             // class field
  static void va(){
     System.out.println(++a);
}
}
class MyP8_67 {
  public static void main(String[] args) {
   System.out.println(vlocal.a);
  }

}
-----------------------------------------------------------------------------------
  

class vinstance {
    int a=100;

    void pa(){
 System.out.println(a);
 }
}
class MyP8_68 {
    public static void main(String[] args) {
    vinstance x = new vinstance();
    System.out.println(x.a);
    x.pa();
  }
}

   

2011年7月4日 星期一

2011-07-04 JAVA

// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class LinearSearch {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j &lt; n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        for (int j = 0; j &lt; n; j++)
            System.out.print(a[j] + " ");
        System.out.println(" }");

        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = 0; i &lt; n; i++)
            if (a[i] == key)
                break;

        if (i &lt; n)                            // 搜尋成功
            System.out.println("該值存在於a[" + i + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");           
    }
}
----------------------------------------------------------------------------------
// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class LinearSearch {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j &lt; n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        //for (int j = 0; j &lt; n; j++)
            //System.out.print(a[j] + " ");
        //System.out.println(" }");
        for (int j : a)
           System.out.print(j + " ");
          
        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = 0; i &lt; n; i++)
            if (a[i] == key)
                break;

        if (i &lt; n)                            // 搜尋成功
            System.out.println("該值存在於a[" + i + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");           
    }
}
--------------------------------------------------------------------------------------
// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class MyP6_7 {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j &lt; n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        //for (int j = 0; j &lt; n; j++)
            //System.out.print(a[j] + " ");
        //System.out.println(" }");
        for (int j : a)
           System.out.print(j + " ");
         
        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = n-1; i &gt;= 0; i--)  //倒回來找
            if (a[i] == key)
                break;

        if (i &lt; n)                            // 搜尋成功
            System.out.println("該值存在於a[" + (i+1) + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");          
    }
}
-----------------------------------------------------------------------------------
// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class MyP6_7 {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        final int n = 12;        // 元素數
        int[] a = new int[n];        // 宣告陣列

        for (int j = 0; j &lt; n; j++)
            a[j] = rand.nextInt(10);

        System.out.print("陣列a的所有元素的值\n{ ");
        //for (int j = 0; j &lt; n; j++)
            //System.out.print(a[j] + " ");
        //System.out.println(" }");
        for (int j : a)
           System.out.print(j + " ");
         
        System.out.print("搜尋的數值:");
        int key = stdIn.nextInt();

        int i;
        for (i = n-1; i &gt;= 0; i--)  //倒回來找
            if (a[i] == key)
                break;

        if (i &lt; n &amp;&amp; i!=-1)                            // 搜尋成功
            System.out.println("該值存在於a[" + (i+1) + "]。");
        else                // 搜尋失敗
            System.out.println("該值不存在。");          
    }
}
----------------------------------------------------------------------------------------
// 將陣列元素的順序逆向重整並顯示

import java.util.Random;
import java.util.Scanner;

class ReverseArray {

    public static void main(String[] args) {
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);

        System.out.print("元素數:");
        int n = stdIn.nextInt();                // 輸入元素數
        int[] a = new int[n];                    // 宣告陣列

        for (int i = 0; i &lt; n; i++) {
            a[i] = 10 + rand.nextInt(90);
            System.out.println("a[" + i + "] = " + a[i]);
        }

        for (int i = 0; i &lt; n / 2; i++) {
            int t = a[i];
            a[i] = a[n - i - 1];     //a[i]與a[n-i-1] swap交換
            a[n - i - 1] = t;
        }

        System.out.println("將元素的排列順序逆轉。");
        for (int i = 0; i &lt; n; i++)
            System.out.println("a[" + i + "] = " + a[i]);
    }
}
----------------------------------------------------------------------------------------
// 線性搜尋

import java.util.Random;
import java.util.Scanner;

class MyP6_12 {

    public static void main(String[] args) {
        int[] a = {0,1,2,3,4,5,6,7,8,9};
       
        for(int i=0 ; i&lt;10 ; ++i){   //i&lt;10,跑幾次,不一定是10
           int r = rand.nextInt(9);  //0~8
          
           int t;
           t=a[r];
           a[r]=a[r+1];
           a[r+1]=t;
          
          
        }
           for (int v :a)
              System.out.print(v+",");

     }   
   
   
   
    }
---------------------------------------------------------------------------------------------
/**
 * 處理檔案及目錄
 *
 * @author (Shieh-Fu Chen)
 * @version (V 1.0  2011/06/27)
 *
 * 執行結果
 * ======================================================================
 * CD=D:\bobe\toBalaTech\Develop\Java\java2009\tools\npp587
 * D:\\fred.txt
 */
import java.util.Scanner;
import java.io.*;
public class MyFile2 {
   public static void main (String args[]) throws IOException   {

     System.out.println("user.dir = " + System.getProperty("user.dir"));  // JVM環境變數;取得 user.dir 環境變數的值
     System.setProperty("user.dir", "D:/");                       // 改變 user.dir 環境變數的值
       
     // java.io.File 這類別主要是處理檔案及目錄的 PathName, 而不是處理檔案的 I/O (開檔, 讀檔, 存檔),
     // 下式只是取得 fred.txt 完整目錄名稱, 並沒有建立新檔
     String s = new File("fred.txt").getAbsolutePath();  //File class;檔案不存在一樣成立
     System.out.println(s);
   
     if (new File(s).exists()) {
        Scanner stdIn = new Scanner(System.in);
       
        System.out.println("進行倒數: ");
        int x;
       
        System.out.print("正整數值: ");
        x = stdIn.nextInt();
       
        if (x == 0)
          System.out.println(new File(s).delete());
     }
       System.out.println("檔案不存在");
   }
}
-------------------------------------------------------------------------------------
/**
 * 建立文字檔, 並寫入字串資料
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 */

import java.util.Scanner;
import java.io.*;

class MyWriter1 {
   public static void main(String[] argv) throws IOException {
  
      // PrintWriter 沒有提供 Append File 的建構子     
      PrintWriter pWriter = new PrintWriter("d:\\MyWriter1.txt"); //PrintWriter產生一檔案,如有`存在檔案則將內容清空
      Scanner stdIn = new Scanner(System.in);

      System.out.print("ID:");
      String s = stdIn.nextLine(); //nextLine輸入字串,空白亦可

      System.out.print("Score:");
      int a = stdIn.nextInt();                       

      // 寫入檔案
      pWriter.print(s+",");
      pWriter.print(a);

      // 關閉檔案
      pWriter.close();
   }
}
---------------------------------------------------------------------------------------
/**
 * 將輸入成績, 附加在檔案後面
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 *
 */

import java.util.Scanner;
import java.io.*;

class MyWriter2 {
   public static void main(String[] argv) throws IOException{

        // FileWriter 有提供 Append File 的建構子, 但是寫入的方法 (write()) 只提供字元及字串資料型態
        FileWriter fWriter = new FileWriter("d:\\MyWriter2.txt", true);  //FileWriter--&gt;true資料附加
       
          Scanner stdIn = new Scanner(System.in);

        System.out.print("Name:");
        String n = stdIn.nextLine();

        System.out.print("Address:");
        String a = stdIn.nextLine();                       
       
        fWriter.write(n+",");   // write() 無法寫入 double 資料
        fWriter.write(a);
        fWriter.write("\n");   // 在 記事本 中, 並沒有換行, 應如何解決 ?  在檔案是以ASC碼的10,13(微軟),其它OS為10         

        fWriter.close();

   }
}
-----------------------------------------------------------------------------------------------------
/**
 * 輸入五位同學, 國英數三科成績,存入文字檔
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 *
 */

import java.util.Scanner;
import java.io.*;

class MyWriter3 {
   public static void main(String[] argv) throws IOException {

        // FileWriter 有提供 Append File 的建構子
        FileWriter fWriter = new FileWriter("d:\\MyWriter3.txt", true);
       
        // PrintWriter 有提供好用的寫入方法 print()
        PrintWriter pWriter = new PrintWriter(fWriter);
       
          Scanner stdIn = new Scanner(System.in);

        System.out.print("學號:");
        String n = stdIn.nextLine();

        System.out.print("國文:");
        int c = stdIn.nextInt();    
       
        System.out.print("英文:");
        int e = stdIn.nextInt(); 
       
        System.out.print("數學:");
        int m = stdIn.nextInt(); 
       
        pWriter.print(n+","); // 寫入字串
        pWriter.print(c+","); // 寫入整數
        pWriter.print(e+","); // 寫入整數
        pWriter.print(m);     // 寫入整數
       
        // 根據不同作業系統, 寫入正確的換行碼
        pWriter.print(System.getProperty("line.separator")); //取得JVM環境變數            

        pWriter.close();
   }
}
----------------------------------------------------------------------------------------------------------
/**
 * 讀出文字檔內容
 *
 * @author (Sung-Lin Chen)
 * @version (V 0.1  2011/04/22)
 */

import java.io.*;

public class MyReader1 {
    public static void main(String[] args) throws IOException {
        FileReader fileReader = new FileReader("d:\\MyWriter2.txt"); //FileReader讀入檔案;如檔案不存在, 則無法RUN;
        BufferedReader bReader = new BufferedReader(fileReader); //緩衝;提供很好用的METHOD readLine()
       
        String s;
        while((s = bReader.readLine()) != null ) {
           System.out.println(s);
        }
        bReader.close();
    }

}
--------------------------------------------------------------------------------------------------------
import java.util.Scanner;
import java.io.*;
class MyReaderWriter {
     public static void main(String[] args)throws IOException{
        FileWriter fWriter = new FileWriter("d:\\out.txt", false);
        FileReader fileReader = new FileReader("d:\\MyWriter2.txt");
        BufferedReader bReader = new BufferedReader(fileReader);
       
       
     String s;   
        while((s = bReader.readLine()) != null ) {
           for (int i=0; i &lt; s.length() ; i++) {
             char c = s.charAt(i);
             fWriter.write(c==',' ? '$' : c);
             /* /* if ( c == ',' )
                System.out.print('$');
             else
                System.out.print(c); */
           }     
           fWriter.write(System.getProperty("line.separator"));   
        }
       
        fileReader.close();
        fWriter.close();       

}
}
---------------------------------------------------------------------------------------
/**
 * 讀出文字檔內容, 並解析單行文字
 *
 * @author (Shieh-Fu Chen)
 * @version (V 0.1  2011/07/04)
 *
 */

import java.io.*;

public class dafumenu {
    public static void main(String[] args) throws IOException{

        FileReader fileReader = new FileReader("d:\\dafumenu.txt");
        BufferedReader bReader = new BufferedReader(fileReader);
       
        String s;   
        while((s = bReader.readLine()) != null ) {
            if (s.equals("[system]"))
              System.out.println("system");
            else if (s.equals("[account]"))
              System.out.println("account");
            else if (s.equals("[student]"))
              System.out.println("student");
            else
               System.out.println();
           }   
              fileReader.close();
        }
       
        //fileReader.close();
   
}

2011年6月30日 星期四

2011-07-01 JAVA

1.http://www.ithome.com
   code review(程式碼審查)
   Design Pattern

class MyP5_66 {
     public static void main(String[] args) {
          System.out.println(Float.toHexString(3.75f));
   
     }

}
-------------------------------------------------------------------------
// 從0.0開始遞增0.001至1.0為止,並且顯示其總和(以float控制迴圈)

class FloatSum1 {

    public static void main(String[] args) {
        float sum = 0.0F;

        for (float x = 0.0F; x <= 1.0F; x += 0.1F) {
            System.out.println("x = " + x);
            sum += x;
        }
        System.out.println("sum = " + sum);
    }
}
------------------------------------------------------------------------------
// 因輸出回列首而改寫顯示完成的字元

class CarriageReturn {

    public static void main(String[] args) {
        System.out.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        System.out.println("\r12345");
    }
}
-----------------------------------------------------------------------------------
// 因輸出回列首而改寫顯示完成的字元

class CarriageReturn {

    public static void main(String[] args) {
        System.out.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        System.out.println("\r12345\\"); //出現\
    }
}
--------------------------------------------------------------------------------------
// 輸入陣列所有元素的值並顯示

import java.util.Scanner;

class IntArrayScan {

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);

        System.out.print("元素數:");
        int n = stdIn.nextInt();            // 輸入元素數
        int[] a = new int[n];                // 產生陣列

        for (int i = 0; i < n; i++) {
            System.out.print("a[" + i + "] = ");
            a[i] = stdIn.nextInt();
        }

        for (int i = 0; i < n; i++)
            System.out.println("a[" + i + "] = " + a[i]);
    }
}
-----------------------------------------------------------------------------------
// 以1、2、3、4、5初始化陣列的各個元素後顯示

class IntArrayInit {

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5}; 

        for (int i = 0; i < a.length; i++)
            System.out.println("a[" + i + "] = " + a[i]);
    }
}
---------------------------------------------------------------------------------
int[] a;
a = new int[] {1,2,3,4,5};
--------------------------------------------------------------------------------
// 以1、2、3、4、5初始化陣列的各個元素後顯示

class IntArrayInit {

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        a = new int[] {10,20,30,40,50};
        for (int i = 0; i < a.length; i++)
            System.out.println("a[" + i + "] = " + a[i]);
    }
}
-------------------------------------------------------------------------------
import java.util.Scanner;
class MyP6_5 {
     public static void main(String[] args) {
          Scanner stdIn = new Scanner(System.in);
         
          System.out.print("元素數:");
          int n = stdIn.nextInt();
          int[] a = new int[n];
         
          for (int i = 0; i < n ; i++) {
              System.out.print("a[" + i + "] = ");
              a[i] = stdIn.nextInt();
             
          }
          System.out.print("a = {");
          for (int i = 0 ; i < n-1; i++)
              System.out.print(a[i] + ",");
             
              System.out.println(a[n-1]+"}");
   
     }


}
-------------------------------------------------------------------------
// 求出並顯示陣列所有元素的總和(強化型for敘述)

class ArraySumForIn {

    public static void main(String[] args) {
        double[] a = { 1.0, 2.0, 3.0, 4.0, 5.0 };

        for (int i = 0; i < a.length; i++)
            System.out.println("a[" + i + "] = " + a[i]);

        double sum = 0;        // 總和
        for (double i : a)
            sum += i;

        System.out.println("所有元素的總和為" + sum + "。");
    }
}
---------------------------------------------------------------------------------

http://javaangrybird.blogspot.com