1. 程式人生 > >字串,字元陣列,整型陣列,List之間的轉換應用

字串,字元陣列,整型陣列,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);