List<String> blist=new ArrayList<String>(); //创建一个list集合
String [] a=new String[blist.size()]; //创建string类型数组
blist.toArray(a);//将list集合转成string数组
//遍历输出string数组
Arrays.stream(a).forEach(System.out::println);
String[]arr = new String[]{"123","345","456"};
//转为ArrayList
List<String> list = new ArrayList<>(Arrays.asList(arr));
list.add("567");
System.out.println(list);
如果使用下面这种转换需要注意了,这样的是不具备增删的功能的
String[]arr = new String[]{"123","345","456"};
List<String> list = Arrays.asList(arr);
System.out.println(list);
使用这种方式转来的list的类型是Arrays的一个内部类,拥有的方法数量有限,不具备add 、remove等的常用操作。(虽然这个内部类也叫ArrayList)
list.add("567"); //会报UnsupportedOperationException异常
若要经转化后有增加删除等操作,可转为ArrayList或其他拥有完整操作的list类。
注意
:对于int[]数组不能直接这样做,因为asList()方法的参数必须是对象。应该先把int[]转化为Integer[]。对于其他primitive类型的数组也是如此,必须先转换成相应的wrapper类型数组。
int[] numbers = {7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4};
int size = numbers.length;
Integer[] array = new Integer[size];
for (int i = 0; i < numbers.length; i++) {
Integer integer = numbers[i];
array[i] = integer;
}
List list = Arrays.asList(array);
String [] a={"abc","d","ef"};
String str=StringUtils.join(a,",")// 使用逗号隔开
join方法的源码:
/**
* Joins the elements of the provided array into a single String
* containing the provided list of elements.
*
* No delimiter is added before or after the list.
* Null objects or empty strings within the array are represented by
* empty strings.
*
*
* StringUtils.join(null, *) = null
* StringUtils.join([], *) = ""
* StringUtils.join([null], *) = ""
* StringUtils.join(["a", "b", "c"], ';') = "a;b;c"
* StringUtils.join(["a", "b", "c"], null) = "abc"
* StringUtils.join([null, "", "a"], ';') = ";;a"
*
*
* @param array the array of values to join together, may be null
* @param separator the separator character to use
* @return the joined String, {@code null} if null array input
* @since 2.0
*/
public static String join(final Object[] array, final char separator) {
if (array == null) {
return null;
}
return join(array, separator, 0, array.length);
}
可以看到join方法中的第二个参数分隔符,即是数组转字符串时,使用分隔符隔开。
//Original String
String string = "hello world";
//Convert to byte[]
byte[] bytes = string.getBytes(); //或者getBytes("utf-8")
//Convert back to String
String s = new String(bytes);
// 可直接返回当前时间的毫秒数
long now = System.currentTimeMillis();
System.out.println("now:" + now);
// 指定date返回long类型
Date date = new Date();
long time = date.getTime();
System.out.println("time:"+ time);
/*
Output:
now:1550375633383
time:1550375935743
*/
Date date = new Date();
System.out.println(date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(date);
System.out.println(format);
/*
Output:
Sun Feb 17 11:49:27 CST 2019
2019-02-17 11:49:27
*/
simpleDateFormat.parse(dateStr)
String dateStr = "2019-02-17 11:58:55";
try {
Date parse = simpleDateFormat.parse(dateStr);
System.out.println(parse);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
Output:
Sun Feb 17 11:58:55 CST 2019
*/
String string = "1550376176762";
long parseLong = Long.parseLong(string);
System.out.println(parseLong);
Long.toString()
最快 String string2 = Long.toString(parseLong);
System.out.println(string2);
String string = "1550376176762";
long parseLong = Long.parseLong(string);
Date date3 = new Date(parseLong);
System.out.println("date3:" + date3);
a. Array是表态连续分配的一片内存区域,与ArrayList相比、不能动态改变大小,通过Arrays进行sort、binarySearch等操作;
b. ArrayList是继承自List的可动态改变大小的数组,和Array一样要求连续分配,内部封闭了一个Object数组,许多方法直接调用Arrays实现;
c. Vector和ArrayList功能基本一致,但Vector是线程安全的。
效率由高到低依次为:Array、ArrayList、Vector。
List 转换成 String :
迭代List的每个子项,将他们用“,”隔开,拼接最好是要用StringBuilder 或者StringBuffer
来完成,当然"String+=" 也是可以的:
public static String listToString(List<String> list){
if(list==null){
return null;
}
StringBuilder result = new StringBuilder();
boolean first = true;
//第一个前面不拼接","
for(String string :list) {
if(first) {
first=false;
}else{
result.append(",");
}
result.append(string);
}
return result.toString();
}
String 转换成List
:
首先String类没有提供直接转换出List的功能;
String提供了一个根据字符来分割字符串的功能,但是分割的结果是String[]类型的,如果我们需要的是String[]类型,那么正好直接使用,
需要List可以使用如下的方法:
public static List<String> stringToList(String strs){
String str[] = strs.split(",");
return Arrays.asList(str);
}
更简洁的list
Joiner.on(",").join(list);
public static void main(String[] args) {
List<String> name =new ArrayList<>(Arrays.asList("xxx","yyy","zzz"));
name.add("aaa");
name.add("bbb");
String nameStr = Joiner.on(",").join(name);
System.out.println(nameStr);//xxx,yyy,zzz,aaa,bbb
List<String> dogs = new ArrayList<String>();
dogs.add("Aigi");
dogs.add("Spitz");
dogs.add("Mastiff");
dogs.add("Finnish Spitz");
dogs.add("Briard");
String dogStr = Joiner.on(",").join(name);
System.out.println(dogStr);//Aigi,Spitz,Mastiff,Finnish Spitz,Briard
String str[] = dogStr.split(",");
System.out.println(Arrays.asList(str));//[Aigi, Spitz, Mastiff, Finnish Spitz, Briard]
}