Java中不同数据类型的相互转换

int转Integer

public class Main {
    public static void main(String[] args) {
        int num = 10;
        Integer newNum = num; //自动装箱
        Integer newNum2 = new Integer(num); //通过构造方法
        Integer newNum3 = Integer.valueOf(num); //通过包装类的静态方法
    }
}

Integer转int

public class Main {
    public static void main(String[] args) {
        Integer num = new Integer(10);
        int newNum = num; //自动拆箱
        int newNum2 = num.intValue(); //调用包装类的方法
    }
}

int[]转ArrayList<Integer>

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 3, 7, 4, 5};
        List<Integer> list = new ArrayList<>();
        for (int i : arr) {
            list.add(i);
        }
        System.out.println(list.toString()); //[1, 3, 7, 4, 5]
    }
}

Integer[]转ArrayList<Integer>

import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Integer[] arr = new Integer[] {1, 3, 7, 4, 5};
        List<Integer> list = Arrays.asList(arr);
        System.out.println(list.toString()); //[1, 3, 7, 4, 5]
    }
}

String转char[]

public class Main {
    public static void main(String[] args) {
        String s = "abcd";
        char[] value = s.toCharArray();
        for (char c : value) {
            System.out.print(c + " "); //a b c d
        }
    }
}

char[]转String

public class Main {
    public static void main(String[] args) {
        char[] value = new char[]{'a', 'b', 'c', 'd'};
        String s = new String(value); //通过String类的构造方法
        String s2 = String.valueOf(value); //String类的valueOf()方法
    }
}

char[]转ArrayList<Character>

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        char[] value = new char[]{'a', 'b', 'c', 'd'};
        List<Character> list = new ArrayList<>();
        for (char c : value) {
            list.add(c);
        }
        System.out.println(list.toString());
    }
}

String[]转ArrayList<String>

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String[] strs = new String[]{"aa", "bb", "cc", "dd"};
        List<String> list = new ArrayList<>();
        for (String str : strs) {
            list.add(str);
        }
        System.out.println(list.toString()); //[aa, bb, cc, dd]
    }
}

字符串转数字

public class Main {
    public static void main(String[] args) {
        String s = "1101";
        int num = Integer.parseInt(s); //调用相应包装类的parseXxx(String)方法
        System.out.println(num); //1101
        int num2 = Integer.parseInt(s, 2); //二进制字符串转十进制数字
        System.out.println(num2); //13
        int num3 = Integer.parseInt(s, 16); //十六进制字符串转十进制数字
        System.out.println(num3); //4353
    }
}

数字转字符串

public class Main {
    public static void main(String[] args) {
        int num = 101;
        String s = String.valueOf(101);
        String s2 = num + "";
        String s3 = Integer.toString(num); //十进制数字转换为十进制表示的字符串
        System.out.println(s3); //101
        String s4 = Integer.toString(num, 2); //十进制数字转换为二进制表示的字符串
        System.out.println(s4); //1100101
        String s5 = Integer.toString(num, 16); //十进制数字转换为十六进制表示的字符串
        System.out.println(s5); //64
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值