Java中String和List转换方法

常用的String字符串和List的相互转化,直接上代码,看总结。

package com.test01.listtostring;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;

public class ListToString {
/*
* 方法一:List转String
* */
@SuppressWarnings(“unused”)
@Test
public void listToString01(){
List listStr = new ArrayList();
listStr.add(“01”);
listStr.add(“02”);
listStr.add(“03”);

		if (listStr == null) {
			System.out.println("listStr为空");
		}else{
			StringBuilder sb = new StringBuilder();
			boolean flag = false;
			for (String string : listStr) {
				if (flag) {
					sb.append(",");
				} else {
					flag = true;
				}
				sb.append(string);
			}
			System.out.println(sb.toString()); //01,02,03
		}
	}

/*
 * 方法二:List转String
 * 	List先转成String数组,在通过String[]数据转换成String
 * */
@Test
public void listToString02(){
		List listStr = new ArrayList();
		listStr.add("a");
		listStr.add("b");
		listStr.add("c");
		
		String[] strArr = listStr.toArray(new String[listStr.size()]);
		for(String str : strArr){
			str = str+",";
			System.out.print(str);//a,b,c,
		}
	}

/*
 * 方法三:List转String和上面的方法只是生命ArrayList对象不一样
 * 	List先转成String数组,在通过String[]数据转换成String
 * */
@Test
public void listToString03(){
		List listStr = new ArrayList();
		listStr.add("a");
		listStr.add("b");
		listStr.add("c");
		
		//List转String
		String[] strs2 = new String[listStr.size()];
		for(int i=0;i listStr = new ArrayList();
	listStr.add("aa");
	listStr.add("bb");
	listStr.add("cc");
	
	String result = "";
	//List转String
	if(listStr != null && listStr.size() > 0){
		String[] strs2 = listStr.toArray(new String[listStr.size()]);
		for(int i = 0;i < strs2.length;i++){
			if(i < strs2.length-1){
				result += strs2[i] + ",";
			}else{
				result += strs2[i];
			}
		}
		System.out.println(result);
	}else{
		System.out.println("List is null!");
	}
	
}

/*
 * 方法五:List转String
 * */
@Test
public void listToString05(){
	List listStr = new ArrayList();
	listStr.add("aa");
	listStr.add("bb");
	listStr.add("cc");
	
	final String SEPARATOR = ",";
	StringBuffer sb = new StringBuffer();
	String result = "";
	
	if(listStr != null && listStr.size()>0){
		//System.out.println(listStr);
		for(String s : listStr){
			sb.append(s);
			sb.append(SEPARATOR);
		}
		result = sb.toString();
		result = result.substring(0, result.length()-SEPARATOR.length());
	}else{
		result = "List is null!!!";
	}
	System.out.println(result);
	
}

/*
 *方法一:String转换成List
 * */
@Test
public void StringToList01(){
	String[] strs = {"aa","bb","cc"};
	List strsToList = Arrays.asList(strs);
	System.out.println("String转换List后,List的值:"+strsToList);
	
}

/*
 *方法二:String转换成List
 * */
@Test
public void StringToList02(){
	String[] strs = {"aa","bb","cc","dd"};
	List strsToList = new ArrayList();
	Collections.addAll(strsToList,strs);
	System.out.println("String转换List后,List的值:"+strsToList);
	
}

/*
 *方法三:String转换成List
 * */
@Test
public void StringToList03(){
	String[] strs = {"aa","bb","cc","dd","ee"};
	List strsToList = new ArrayList<>();
	
	for(String s : strs){
		strsToList.add(s);
	}
	System.out.println("String转换List后,List的值:"+strsToList);
	
}

}

总结:
1.List转换成String字符串的思想:
(1)List先转换成String[ ]数组,再把String[ ]数组转化成String字符串;
(2)List先转换成StringButter,再把StringButter通过toString()转化成String字符串;

2.String字符串转换成List的思想:
String先转化成String[ ]数组,再通过String[ ]转化成List;

你可能感兴趣的:(Java基础)