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

时间:2023-05-15 09:39:26
如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 
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[]);