Java programming cheat sheet


//        1,数组和list之间的转换

//1) String[] 转换成 list
        String[] strArr = {"a", "b"};
        List list = Arrays.asList(strArr);
        System.out.println(list);

//1.5) int[] 转换成 list
        int[] arr = {0,1};
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            list.add(arr[i]);
        }
        System.out.println(list);
//或者
        List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());

//2) list 转 array
        List<Integer> al = new ArrayList<>();
        al.add(10);
        al.add(20);
        al.add(30);
        al.add(40);
        Integer[] arr = new Integer[al.size()];
        arr = al.toArray(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        不能直接Integer[] objects = al.toArray();

//3)two dimensional array 转 list
        int[][] array = {{0,1},{1,2},{2,0},{1,3}};
        List<List<Integer>> lists = new ArrayList<>();
        for (int[] ints : array) {
            List<Integer> list = new ArrayList<>();
            for (int i : ints) {
                list.add(i);
            }
            lists.add(list);
        }

//下面这种写法内部并没有转
        int[][] array = {{0,1},{1,2},{2,0},{1,3}};
        List list = Arrays.asList(array);
        for (int i = 0; i < list.size(); i++) {
            int[] arr = (int[]) list.get(i);
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[j]);
            }
            System.out.println();
        }

//4)two dimensional list 转 array
        List<List<String>> lists = new ArrayList<>();
        List<String> list1 = Arrays.asList("a", "b", "c");
        List<String> list2 = Arrays.asList("d", "e");
        lists.add(list1);
        lists.add(list2);
        String[][] strArray = new String[lists.size()][];
        for (int i = 0; i < lists.size(); i++) {
            List<String> row = lists.get(i);
            strArray[i] = row.toArray(new String[row.size()]);
        }






//        2. list组成的数组和数组组成的list

        //list组成的数组
        List<Integer>[] graph = new ArrayList[3];
        for (int i = 0; i < graph.length; i++) {
            graph[i] = new ArrayList<>();
        }
        graph[0].add(1);
        for (int i = 0; i < graph.length; i++) {
            for (int j : graph[i]) {
                System.out.println(j);
            }
        }

        //数组组成的list
        List<int[]> graph = new ArrayList<>();
        for (int[] array : graph) {
            for (Integer i : array) {
                System.out.println(i);
            }
        }



//        3,遍历HashSet,HashMap

        Set<String> hs = new HashSet<>();
        hs.add("California");
        for (String str : hs) {
            System.out.println(str);
        }


        Map<String, Integer> hm = new HashMap<>();
        hm.put("A", 10);
        for (Map.Entry<String, Integer> entry : hm.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }


//        4,构建图

        //Build list array graph
        int n = 4;
        List<Integer>[] graph = new List[n];
        List<Integer> list0 = new ArrayList<>();
        list0.add(0);
        list0.add(1);
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        list1.add(2);
        List<Integer> list2 = new ArrayList<>();
        list2.add(2);
        list2.add(0);
        List<Integer> list3 = new ArrayList<>();
        list3.add(1);
        list3.add(3);
        graph[0] = list0;
        graph[1] = list1;
        graph[2] = list2;
        graph[3] = list3;


        //Build list list graph
        int n = 4;
        List<List<Integer>> graph = new ArrayList<>();
        List<Integer> list1 = new ArrayList<>();
        list1.add(0);
        list1.add(1);
        List<Integer> list2 = new ArrayList<>();
        list2.add(1);
        list2.add(2);
        List<Integer> list3 = new ArrayList<>();
        list3.add(2);
        list3.add(0);
        List<Integer> list4 = new ArrayList<>();
        list4.add(1);
        list4.add(3);
        graph.add(list1);
        graph.add(list2);
        graph.add(list3);
        graph.add(list4);

        //Build matrix graph
        int n = 8;
        int graph[][] = {
            {0, 0, 1, 0, 0, 0, 0, 0},
            {1, 0, 0, 0, 0, 0, 0, 0},
            {0, 1, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0, 0, 1, 0},
            {0, 0, 0, 0, 0, 0, 0, 1},
            {0, 0, 0, 0, 1, 0, 0, 0}
        };

 

发布了15 篇原创文章 · 获赞 0 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览