ArrayTest

public class ArrayTest {

    public static void main(String[] args) {
        int[] arr = { 34, 12, 56, 100, 90 };
        printArray(arr);
        seleteSort(arr);
        printArray(arr);
    }

    /**
     * 选择排序
     */
    public static void seleteSort(int[] arr) {
        for (int x = 0; x < arr.length - 1; x++) {
            for (int y = x + 1; y < arr.length; y++) {
                if (arr[x] > arr[y]) {
                    displace(arr, x, y) ;
                }
            }
        }
    }

    /**
     * 置换功能.
     */
    private static void displace(int[] arr,int a,int b) {
        int temp = arr[a] ;
        arr[a] = arr[b] ;
        arr[b] = temp ;
    }

    /**
     * 打印功能
     */
    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int x = 0; x < arr.length; x++) {
            if (x != arr.length - 1) {
                System.out.print(arr[x] + ", ");
            } else {
                System.out.println(arr[x] + "]");
            }
        }
    }
}

public class ArrayTest2 {

    public static void main(String[] args) {
        int[] arr = { 23, 45, 67, 75, 89, 98, 100, 106 };
        int index = halfSearch(arr, 23);
        System.out.println("index = " + index);

        int index2 = Arrays.binarySearch(arr, 23) ; //java自带的方法.
        /*
         * 如果存在,返回的是数值的角标位置.如果不存在,则表现为负的角标并且减1.
         */
        System.out.println("index2 = " + index2); 

    }

    /*
     * 给定一个有序的数组,如果往该数组中存储一个元素,并保证这个数组还是有序的. 那么这个元素的角标如何获取.
     */
    public static int halfSearch(int[] arr, int key) {
        int min, mid, max;
        min = 0;
        max = arr.length - 1;
        mid = (min + max) >> 1;

        while (min <= max) {
            mid = (max + min) >> 1;
            if (key > arr[mid]) {
                min = mid + 1;
            } else if (key < arr[mid]) {
                max = mid - 1;
            } else {
                return mid;
            }
        }
        return min;
    }
}

/*
 * 获取一个整数的16进制表现形式.
 */
public class ArrayTest3 {

    public static void main(String[] args) {
        toHex_3(0);

    }

    public static void toHex_3(int num) {
        if(num == 0) {
            System.out.println("0");
            return ;
        }
        //定义一个对应关系表
        char[] chs = { '0', '1', '2', '3', '4', '5', 
                '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        /*
         * 如果查表会查到比较多的数据.
         * 数据一多,就先存储起来,再进行操作.
         * 所以定义一个数组.叫临时容器.
         */
        char[] arr = new char[8] ;
        int pos = arr.length ;
        while(num != 0) {
            int temp = num & 15 ;
            arr[--pos] = chs[temp] ;
            num = num >>> 4 ;
        }

        System.out.println("pos=" + pos);
        for(int x=pos; x<arr.length; x++) {
                System.out.print(arr[x]);
        }

    }

    /*
     * 什么时候使用数组呢? 如果数据出现了对应关系,而且对应关系的一方是有序的数字编号. 
     * 并作为角标使用. 这时候就必须要想到数组的使用.
     * 就可以将这些数据存储到数组中. 根据运算的结果作为角标直接去查数组中对应的元素即可. 
     * 这种方式:称为查表法.
     */

    public static void toHex_2(int num) {
        char[] chs = { '0', '1', '2', '3', '4', '5', 
                '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        for (int x = 0; x < 8; x++) {
            int temp = num & 15 ;
            System.out.print(chs[temp]);
            num = num >>> 4 ;
        }
    }

    public static void toHex_1(int num) {

        for (int x = 0; x < 8; x++) {
            int temp = num & 15;
            if (temp > 9) {
                System.out.print((char) (temp - 10 + 'A'));
            } else {
                System.out.print(temp);
            }
            num = num >>> 4;
        }
        /*
         * int n1 = num & 15 ; 
         * System.out.println("n1= " + n1);
         * 
         * num = num >>> 4 ; 
         * int n2 = num & 15 ; System.out.println("n2= " + n2);
         */
    }
}

public class ArrayTest4 {

    public static void main(String[] args) {
        String week = getWeek(1) ;
        System.out.println(week);
    }

    /*
     * 使用查表法.
     * 星期
     */
    public static String getWeek(int num) {

        if(num>7 || num<1) {
            return "非法的输入" ;
        }
        String[] weeks = {"","星期一","星期二","星期三","星期四","星期五",
                "星期六","星期天"} ;

        return weeks[num] ;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值