java基础转换

    /**
     * 长度填充
     */
    public void StringFormat() {
        String.format("%5d", 123);//输出"  123" =>右侧对齐,左侧不足5位的的地方补空格
        String.format("%-5d", 123);//输出"123  " => 左侧对齐,右侧不足5位的地方用空格补齐
    }


/**
     * 进制转换
     */
    public static void radioConversion() {
        //二进制转十进制
        String binaryVal = "0000010010001110";
        int decVal = Integer.parseInt(binaryVal, 2);
        //十进制转十六进制
        String hex16 = Integer.toHexString(decVal);

        //十六进制转十进制
        String hex16Val = "48E";
        Integer dec10Val = Integer.parseInt(hex16Val, 16);

        //十进制转二进制
        String binVal = Integer.toBinaryString(dec10Val);
    }

    /**
     * ASCII码转换相关
     */
    public static void AsciiEx () {
        //字符转ASCII码
        char ch = 'A';
        System.out.println("字符转ASCII码Val " + (int) ch);

        //ASCII码转字符
        int code = 98;
        char codeC = (char) code;
        System.out.println("Ascii转字符 " + codeC);

        //数字转字符
        int num = 5;
        char numC = (char) (num + '0');

        //字符串转ASCII数组
        String str = "Test";
        int [] array = new int[str.length()];
        for (int i = 0; i < str.length(); i++) {
            array[i] = str.charAt(i);
        }

        // ASCII数组转字符串
        byte [] bytes = {72,101,108,108,111};
        System.out.println("ASCII数组转字符串 " + new String(bytes, StandardCharsets.US_ASCII));

        String asciiStr = "hello";
        List<Integer> asciiValList = new ArrayList<>();
        for (int i = 0; i < asciiStr.length(); i++) {
            int asciiVal = asciiStr.charAt(i);
            asciiValList.add(asciiVal);
        }
   
    }

    /**
     * 输出26个字母
     */
    public static void export26ZiMu() {
        List<String> list = new ArrayList<String>();
        for(char c = 'A'; c <= 'Z'; c++) {
            list.add(c + "");
        }
    }


        /**
     * 数组排序
     * @param prices
     */
    public void arraySort(int [] prices) {
        Arrays.sort(prices);
        Integer [] integerAttr = new Integer[prices.length];
        for (int i = 0; i < prices.length; i++) {
            integerAttr[i] = prices[i];
        }
        Arrays.sort(integerAttr, Collections.reverseOrder());
        Collections.sort(new ArrayList<Integer>(), Collections.reverseOrder());

        //list数组排序
        List<int []> list = new ArrayList<>();
        //1. int数组中是一个值
        Collections.sort(list, Collections.reverseOrder());

        //2. int数组中是两个值
        Collections.sort(list, (a, b) -> {
            if (a[0] != b[0]) {
                return a[0] - b[0];
            } else {
                return a[1] - b[1];
            }
        });

      }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值