JavaSE

  • 1.动静态创建数组
//创建数组
        //动态创建
        int [] a = new int [10];
        int a0 [] = new int [20];
        //静态创建
        int a1 []= {1,2,3,4};
        for(int i = 0; i < 10; i ++) {
            a [i] = 2*i+1;
            System.out.println(a[i]);
        }
        int [] a2 = new int [26];
        for(int i = 0, a3 = 97; i < a2.length; i ++, a3 ++) {
            System.out.println((char)(a2[i]+a3));
        }
    1. .length
 int a [][] = {{1,2,3},{4,5},{6,7},{8,9}};
         for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.println(a[i][j]);
            }
        }
    1. arraycopy,arraysort,array toString, arrayequals,array binarySearch
>arraycopy
        String s [] = {"1","2","3","4","5","6","7"};
    String sn [] = new String [10]; 
    System.arraycopy(s, 0, sn, 0, s.length);
    for(int i = 0; i < 10; i ++) {
    System.out.println(sn[i]);
    }
        System.out.println("----------------------");
> Arrays.equals  Arrays.sort()  Arrays.toString()
int a[] = { 1, 2, 0, 54, 74, 1, 4324, 4, 10, 40, -1, -999 };
        int b[] = { 1, 2, 0, 54, 74, 1, 4324, 4, 10, 40, -1, -999 };
        // 比较数组是否相等
        boolean f = Arrays.equals(a, b);
        System.out.println(f);
        // 排序
        Arrays.sort(a);
        // 打印数组的内容
        System.out.println(Arrays.toString(a));
>Arrays.binarySearch
                int a[] = { 1, 2, 0, 54, 74, 1, 4324, 4, 10, 40, -1, -999 };
        int index = Arrays.binarySearch(a, -100);
        System.out.println(index);
  • 4.冒泡排序
int a [] = {4,5,78,79,45,21,46,87,989,54,564,648,5847};
        for(int i = 0; i < a.length; i ++) {
            for(int j = 0; j < a.length-i-1; j ++) {
                if(a[j]>a[j+1]) {
                    int temp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = temp;
                }
            }   
        }
        for(int i : a)
            System.out.println(i);
  • 5.可变参数
                 // 可变参数技术
        // 可变参数是不是数组 ? 原理上讲不是数组
        // 怎么处理可变参数 ? 和处理数组没有区别
        int n = 1,m = 2;
        int r = add(m,n,1,2);
        System.out.println(r);
    }
    public static int add(int ... a) {
        int sum = 0;
        if (a != null) {
            for (int i = 0; i < a.length; i ++) {
                sum += a[i];
            }
        }
        return sum;
    }
}

你可能感兴趣的:(JavaSE)