advertisement

2013年11月28日

JAVA


跳脫字元(Escape character)
\\ -> \
\" -> "
\n -> line feed(換列)
\t -> TAB
\r -> return(回到列首)

//------------------------
基本型別 外覆(Wrap)類別
------------ ---------------

byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean


public class Demo7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int[] a1 = {1,3,5};
        int[] a2 = {2,4,6};
        int[] a3 = {11,22,33};
        //
        int[][] b1={a1,a2,a3};
        System.out.println(b1[1][2]);
     
        //二維陣列宣告
        int[][] b2 = new int[2][3]; //對稱式(矩陣式)陣列結構
        //
        int[][] b3 = new int[3][]; //非稱式(非矩陣式)陣列結構
        b3[0] = new int[]{1,3};
        b3[1] = new int[]{2,4,6};
        b3[2] = new int[4];
     
        //------長度取得
        System.out.println(a1.length);
        System.out.println(b2.length);
        System.out.println(b2[0].length);
        System.out.println(b2[0].length);
        System.out.println(b3[1].length); //非對稱式
        System.out.println(b3[2].length);
     
        //------改變長度
        //a1 = new int[6];
        int[] tmp = new int[6];
        //複製陣列內容(搬家)
        System.arraycopy(a1, 1, tmp, 2, 2);
        a1 = tmp;
        tmp = null;
     
        System.out.println("-------------------");
        //-----流程控制
        for(int i =0; i<b3.length; i++){
           for(int j=0; j<b3[i].length; j++){
               System.out.print(b3[i][j]+" ");
           }
            System.out.println();
        }
        //-------------
        System.out.println("---------------------");
        for(int[] t : b3){
           for(int t2 : t) {
               System.out.print(t2+" ");
           }
            System.out.println();
        }
             
    }
}


public class Demo7B {
    public static void main(String[] args) {
        //請利用datas的資料完成程式、並且輸出如底下所示結果
        // datas[0] -> 用來累績全年銷售業績, data[1]->代表1月銷售業績, data[2]->代表2月
        //銷售業績...等如此依序類
        int[] datas = {0,25,22,23,26,28,29,32,30,28,27,20,25};

    }
}

/*
                    大大服飾公司99年度銷售業績圖
----------------------------------------------------------
1月 *************************(25)
2月 **********************(22)
3月 ***********************(23)
4月 **************************(26)
5月 ****************************(28)
6月 *****************************(29)
7月 ********************************(32)
8月 ******************************(30)
9月 ****************************(28)
10月 ***************************(27)
11月 ********************(20)
12月 *************************(25)
----------------------------------------------------------
銷售總額:??? (萬元)
*/



public class NewMain {
    public static void main(String[] args) {
        //請利用datas的資料完成程式、並且輸出如底下所示結果
        // datas[0] -> 用來累績全年銷售業績, data[1]->代表1月銷售業績, data[2]->代表2月
        //銷售業績...等如此依序類
        int i,j;
        int[] datas = {0,25,22,23,26,28,29,32,30,28,27,20,25};
        System.out.println("                    大大服飾公司99年度銷售業績圖");
        System.out.println("----------------------------------------------");
        for(i=1;i<datas.length;i++) {
            System.out.print(i+"月");
            datas[0]+=datas[i];
            for(j=1;j<=datas[i];j++) {
                System.out.print("*");
            }
            System.out.println("("+datas[i]+")");
        }
        System.out.println("----------------------------------------------");
        System.out.println("銷售總額:"+datas[0]+" (萬元)");
    }
}


public class Demo8 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        for (String t : args) {
            System.out.println("t: " + t);
        }
        
        System.out.println(args[0]+args[1]);
        //將文數字  轉成 數字
        double d1 = Double.parseDouble(args[0]);
        int    i2 = Integer.parseInt(args[1]);
        System.out.println(d1+i2);
    }
}

applet 網頁上的附屬應用程式 故不能寫入讀取 a hard disk of a computer



public class Demo8 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        for (String t : args) {
            System.out.println("t: " + t);
        }
        
        System.out.println(args[0]+args[1]);
        //將文數字  轉成 數字
        double d1 = Double.parseDouble(args[0]);
        int    i2 = Integer.parseInt(args[1]);
        System.out.println(d1+i2);
    }
}



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class Demo9 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList datas = new ArrayList();
        /*
         for(int i=1; i<=49; i++){
         datas.add(i);
         System.out.println("size:"+datas.size());
         }
         */
        Random random = new Random();
        while (datas.size() < 7) {
            int n = random.nextInt(7) + 1;
            if (datas.indexOf(n) == -1) { //搜尋
                datas.add(n); //加入項目至集合中
            }
        }

        for (Object t : datas) {
            System.out.println(t);
        }

        //-------------------------
        int[] d1 = new int[5];
        ArrayList d2 = new ArrayList();
        d1[0] = 11;
        d1[1] = 22;
        d1[2] = 33;
        d1[3] = 44;
        d1[4] = 55;
        //加入
        d2.add(11);
        d2.add(22);
        d2.add(33);
        d2.add(44);
        d2.add(55);
        System.out.println(d1[0]);
        System.out.println(d1[1]);
        //讀取
        System.out.println(d2.get(0));
        System.out.println(d2.get(1));
        
        //移除
        int[] tmp = new int[4];
        System.arraycopy(d1, 0, tmp, 0, 2);
        System.arraycopy(d1, 3, tmp, 2, 2);
        
        d2.remove(33);
        //取得長度
        System.out.println(d1.length);
        System.out.println(d2.size());
        //搜尋
        System.out.println(Arrays.binarySearch(d1, 22));
        System.out.println(d2.indexOf(22));
    }
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class NewMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList datas = new ArrayList();
        /*
         for(int i=1; i<=49; i++){
         datas.add(i);
         System.out.println("size:"+datas.size());
         }
         */
        Random random = new Random();
        while (datas.size() < 7) {
            int n = random.nextInt(7) + 1;
            if (datas.indexOf(n) == -1) { //搜尋
                datas.add(n); //加入項目至集合中
            }
        }

        for (Object t : datas) {
            System.out.println(t);
        }
        System.out.println("-------------------------");
        //-------------------------
        int[] d1 = new int[5];
        ArrayList d2 = new ArrayList();
        d1[0] = 11;
        d1[1] = 22;
        d1[2] = 33;
        d1[3] = 44;
        d1[4] = 55;
        System.out.println("加入");
        //加入
        d2.add(11);
        d2.add(22);
        d2.add(33);
        d2.add(44);
        d2.add(55);
        System.out.println(d1[0]);
        System.out.println(d1[1]);
        System.out.println("讀取");
        //讀取
        System.out.println(d2.get(0));
        System.out.println(d2.get(1));
        System.out.println("移除");
        //移除
        int[] tmp = new int[4];
        System.arraycopy(d1, 0, tmp, 0, 2);
        System.arraycopy(d1, 3, tmp, 2, 2);
        
        d2.remove(2);
        d2.remove((Object)33);
        //d2.remove(());
        System.out.println("取得長度");
        //取得長度
        System.out.println(d1.length);
        System.out.println(d2.size());
        System.out.println("搜尋");
        //搜尋
        System.out.println(Arrays.binarySearch(d1, 22));
        System.out.println(d2.indexOf(22));
    }
}



import java.util.ArrayList;
import java.util.Random;

public class NewMain {

    public static void main(String[] args) {
        ArrayList datas = new ArrayList();

      //  for (int i = 1; i <= 10000; i++) {
        //    datas.add(i);
            // System.out.println("size:" + datas.size());
        //}
        Random random = new Random();
        while (datas.size() < 10000) {
            int n = random.nextInt(49) + 1;
            if (datas.indexOf(n) == -1) { //搜尋
                datas.add(n); //加入項目至集合中
            }
        }
        // int i = 0;
        //for (int x : nums) {
        //   System.out.println(++i + " -> 出現 " + x + " 次");
    }
}

沒有留言:

張貼留言

文章有誤或有問題麻煩您留言告知! 謝謝您~~