Array、List、Set基本操作

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));
    }
}

输出结果:

Array、List、Set基本操作_第1张图片

注意,排序之后会改变原数组的顺序;

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;
    }

输出结果:
Array、List、Set基本操作_第2张图片

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));
    }
}

输出:

Array、List、Set基本操作_第3张图片

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;
    }

输出:

Array、List、Set基本操作_第4张图片

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));
    }

输出:

Array、List、Set基本操作_第5张图片

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;
    }

输出:

Array、List、Set基本操作_第6张图片

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;
    }

输出:

Array、List、Set基本操作_第7张图片

 

你可能感兴趣的:(Java)