• java中如何将字符串数组转换成字符串(转)


    如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 
    String[] str = {"abc", "bcd", "def"};
    StringBuffer sb = new StringBuffer();
    for(int i = 0; i < str.length; i++){
     sb. append(str[i]);
    }
    String s = sb.toString();

    如果是 “字符数组” 转 “字符串” 可以通过下边的方法

    char[]   data={'a','b','c'};   
    String  s=new   String(data);

    //字符数组转List

    String[] s = {"a","b","c"};
    List list= java.util.Arrays.asList(s);


    //List转字符数组——例子一

    List<String> list = new ArrayList<String>();
    list.add("a1");
    list.add("a2");
    String[] toBeStored = list.toArray(new String[list.size()]);
    for(String s : toBeStored) {
    System.out.println(s);
    } 



    //List转字符数组——例子二

    List<String> list = new ArrayList<String>();
    list.add("a1");
    list.add("a2");
    
    String[] toBeStored = new String[al.size()];
    list.toArray(toBeStored);
    for (String s : toBeStored) {
    System.out.println(s);
    } 

    //List转字符数组——例子三

    List<String> list = new ArrayList<String>();
    list.add("a1");
    list.add("a2");
    
    String[] toBeStored = new String[]{};
    String[] str = al.toArray(toBeStored);
    for (String s : str) {
    System.out.println(s);
    } 


    利用此转化:String str[] = list.toArray(new String[]{}); 
    List list= java.util.Arrays.asList(String str[]);

  • 相关阅读:
    pythonchallenge 解谜 Level 6
    pythonchallenge 解谜 Level 5
    pythonchallenge 解谜 Level 4
    pythonchallenge 解谜 Level 3
    pythonchallenge 解谜 Level 2
    pythonchallenge 解谜 Level 1
    CCF 201912-2 回收站选址
    JavaWeb+JDBC+Servlet+SqlServer实现登录功能
    后台连接数据库的方法
    jQuery实现轮播图
  • 原文地址:https://www.cnblogs.com/xuerong/p/5687942.html
Copyright © 2020-2023  润新知