1. 数组排序与输出
demo1:
public class Test1 {
public static void main(String[] args) {
String[] arr = new String[]{"Tom", "Bob", "Jane", "Tom", "Bob"};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
输出结果:
demo2:
public class Test5 {
public static void main(String[] args) {
int[] data = {7, 5, 1, 2, 6, 8, 10, 12, 4, 3, 9, 11, 13, 15, 16, 14};
System.out.println(Arrays.toString(data));
int data1[] = data;
int data2[] = Arrays.copyOf(data,data.length);
Arrays.sort(data);
System.out.println(Arrays.toString(data));
System.out.println(Arrays.toString(data1));
System.out.println(Arrays.toString(data2));
}
}
输出结果:
注意,排序之后会改变原数组的顺序;
2. 数组转List
public static void main(String[] args) {
List list = arr2list();
System.out.println(list);
}
public static List arr2list(){
String[] strArray= new String[]{"Tom", "Bob", "Jane", "Tom"};
List strList= Arrays.asList(strArray);
return strList;
}
3 List转数组
package com.wz.test1;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Auther: 13213
* @Date: 2020/7/12 16:50
* @Description:
*/
public class Test5 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("北京");
list.add("上海");
list.add("广州");
list.add("深圳");
list.add("深圳");
String[] arrays = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrays));
}
}
输出:
4. 数组转set
public static void main(String[] args) {
Set set = arr2set();
System.out.println(set);
}
public static Set arr2set(){
String[] strArray= new String[]{"Tom", "Bob", "Jane", "Tom"};
Set staffsSet = new HashSet<>(Arrays.asList(strArray));
return staffsSet;
}
输出:
5. set转数组
public static void main(String[] args) {
Set set = new HashSet<>();
set.add("1");
set.add("2");
set.add("3");
set.add("4");
set.add("3");
set.add("4");
String[] arr = set.toArray(new String[set.size()]);
System.out.println(Arrays.toString(arr));
}
输出:
6. List转Set
public static void main(String[] args) {
Set set = list2set();
System.out.println(set);
}
public static Set list2set(){
List list = new ArrayList<>();
list.add("北京");
list.add("上海");
list.add("广州");
list.add("深圳");
list.add("深圳");
Set result = new HashSet(list);
return result;
}
输出:
7. Set转List
public static void main(String[] args) {
List list = set2list();
System.out.println(list);
}
public static List set2list(){
String[] staffs = new String[]{"Tom", "Bob", "Jane", "Tom", "Bob"};
Set staffsSet = new HashSet<>(Arrays.asList(staffs));
List result = new ArrayList<>(staffsSet);
return result;
}
输出: