[Java] 数组-05 binarySearch / TestArrayCopy

package com.bjsxt.chap5;

public class binarySearch {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6, 7};
        System.out.println(binarySearch(a, 6));
    }
    public static int binarySearch(int a[], int value) {
        if (a.length == 0) return -1;
        else if (a.length == 1) return 0;
        int start = 0, end = a.length - 1;
        while (start <= end) {
            int k = (start + end) >> 1;
            if (a[k] == value) return k;
            else if (a[k] > value) {
                end = k-1;
            }
            else if (a[k] < value) start = k+1;
            else {
                return k;
            }
        }
        return -1;
    }
}
package com.bjsxt.chap5;

public class TestArrayCopy {
    public static void main(String args[]) {
        String[] s = { "Mircosoft", "IBM", "Sun", "Oracle", "Apple" };
        String[] sBak = new String[6];
        System.arraycopy(s, 0, sBak, 0, s.length);

        for (int i = 0; i < sBak.length; i++) {
            System.out.print(sBak[i] + " ");
        }

        System.out.println();
        int[][] intArray = { { 1, 2 }, { 1, 2, 3 }, { 3, 4 } };
        int[][] intArrayBak = new int[3][];
        System.arraycopy(intArray, 0, intArrayBak, 0, intArray.length);
        intArrayBak[2][1] = 100;

        for (int i = 0; i < intArray.length; i++) {
            for (int j = 0; j < intArray[i].length; j++) {
                System.out.print(intArray[i][j] + "  ");
            }
            System.out.println();
        }
    }
}


posted @ 2013-11-26 18:45  小尼人00  阅读(119)  评论(0编辑  收藏  举报