public class TestArrayToList {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 数组转换为集合Array to List 将字符串数组转换为集合
String[] arr = new String[5];
arr[0] = "a";
arr[1] = "b";
arr[2] = "c";
arr[3] = "d";
arr[4] = "e";
List<String> list = new ArrayList<String>();
list = Arrays.asList(arr);
// 将集合转换为数组
List<String> testList = new ArrayList<String>();
testList.add("A");
testList.add("B");
testList.add("C");
testList.add("D");
testList.add("E");
Object[] objects = testList.toArray();
// 循环遍历数组,获取到数组里面的值
String objectString = "";
for (int o = 0; o < objects.length; o++) {
objectString += objects[o].toString();
}
System.out.println("objectString:" + objectString);
// 将list转换为字符串
String listString = "";
for (int i = 0; i < list.size(); i++) {
listString += list.get(i);
}
System.out.println("listString:" + listString);
// 将数组转换为字符串
String arrString = StringUtils.join(arr);
System.out.println("arrString:" + arrString);
// 对象数组转换为对象集合 JSONArray JSOBObject之间的关系 JSONObject相当于是一个对象,JAONArray就是一个对象数组
// 1.通过JSONObject将String 转换为实体类
Size s = JSONObject.parseObject("{page:123,size:456}", Size.class);
System.out.println("s.getPage():" + s.getPage());
System.out.println("s.getSize():" + s.getSize());
// 对象数组
JSONArray array = JSONArray.parseArray("[{page:123,size:456},{page:112233,size:445566},{page:111222333,size:444555666}]");
System.out.println("array:" + array);
for (int i = 0; i < array.size(); i++) {
JSONObject object = (JSONObject) array.get(i);
System.out.println("object:" + object);
StringBuilder sb = new StringBuilder();
sb.append(object.get("size")).append(",").append(object.get("page"));
System.out.println("sb:" + sb);
}
// 一维对象数组转换为list的情况
// Size s1 = JSONObject.parseObject("[{page:123,size:456},{page:123,size:456},{page:123,size:456}]", Size.class);
JSONArray jsonArray1 = JSONArray.parseArray("[{page:123,size:456},{page:123,size:456},{page:123,size:456}]");// 把String转换为json
// 将JSONArray 转换为list
String jsonStr = JSONObject.toJSONString(jsonArray1);
List<String> aList = JSONObject.parseArray(jsonStr, String.class);
// 把list转换为String
String str4 = StringUtils.join(aList, ",");
System.out.println("str4:" + str4);
// 二维数组转换为list的情况
String string1 = "[[110121,110102,210120],[110121,110102,210120]]";
JSONArray jsonArray2 = JSONArray.parseArray(string1);// 把String转换为json
String jsonStr1 = JSONObject.toJSONString(jsonArray2);
List<List> aList1 = JSONObject.parseArray(jsonStr1, List.class);
String str5 = "";
for (int a = 0; a < aList1.size(); a++) {
str5 += StringUtils.join(aList1.get(a), ",") + (a == aList1.size() - 1 ? "" : "-");
}
System.out.println("str5:" + str5);
}
控制台输出:
objectString:ABCDE
listString:abcde
arrString:abcde
s.getPage():123
s.getSize():456
array:[{"size":456,"page":123},{"size":445566,"page":112233},{"size":444555666,"page":111222333}]
object:{"size":456,"page":123}
sb:456,123
object:{"size":445566,"page":112233}
sb:445566,112233
object:{"size":444555666,"page":111222333}
sb:444555666,111222333
str4:{"size":456,"page":123},{"size":456,"page":123},{"size":456,"page":123}
str5:110121,110102,210120-110121,110102,210120