字符串,字符数组,整型数组,List之间的转换应用

1、字符串——>字符数组:

public class Zhuanhuan {
    public static void main(String []ags){
        String string="fgdshfgdf";
        char []c=string.toCharArray();
        for(char c1:c){
            System.out.println(c1);
        }
    }
}

2、字符数组——>字符串:

public class Zhuanhuan {
    public static void main(String []ags){
      /*  String string="fgdshfgdf";
        char []c=string.toCharArray();
        for(char c1:c){
            System.out.println(c1);
        }*/
        char []c={'s','d','f','g'};
        StringBuffer string=new StringBuffer();
        for(char c1:c){
            string.append(c1);
        }
        System.out.println(string);
    }
}

3、List——>字符串:

        ArrayList<String> list=new ArrayList<String>();
        list.add("fsdfdf");
        list.add("jfklgf");
        StringBuffer stringBuffer=new StringBuffer();
        for(String s:list){
            stringBuffer.append(s);
        }
        System.out.println(stringBuffer);

4、List——>(字符串/字符/整型)数组

ArrayList<String> list=new ArrayList<String>();
        list.add("fsdfdf");
        list.add("jfklgf");
        String []strings=new String[list.size()];
        strings=list.toArray(strings);
        for(String s:strings)
            System.out.println(s);

5、数组排序和List排序:

 Integer []a={1,5,3,6,2,6,9};
        List<Integer> list= Arrays.asList(a);
        Arrays.sort(a);
        Collections.sort(list);
        for(int i:a){
            System.out.println(i);
        }
        System.out.println();
        for(int i:list){
            System.out.println(i);
        }

6、直接输出数组:

Arrays.toString(arr);

7、数组变为List:

List list=Array.asList(arr);


你可能感兴趣的:(库函数之间的转换)