zoukankan      html  css  js  c++  java
  • JavaWeb编程小技巧集合

    在编程时通常会遇到一些问题,有时候是不知所措,有时候是解决方案不够优雅,本篇旨在记录编程过程中一些个人想不到的,稍微优雅一点的解决方案,方案来源均来自互联网。

    List使用subList实现分页获取

            int subSize = 1000;
            int subCount = list.size();
            int subPageTotal = (subCount / subSize) + ((subCount % subSize > 0) ? 1 : 0);
            // 根据页码取数据
            for (int i = 0, len = subPageTotal - 1; i <= len; i++) {
                // 分页计算
                int fromIndex = i * subSize;
                int toIndex = ((i == len) ? subCount : ((i + 1) * subSize));
                List<String> strings = list.subList(fromIndex, toIndex);
            }
    

    ArrayList和数组int[]的相互转化

            int[] data = {4, 5, 3, 6, 2, 5, 1};
            // int[] 转 List<Integer>
            List<Integer> list1 = Arrays.stream(data).boxed().collect(Collectors.toList());
            // Arrays.stream(arr) 可以替换成IntStream.of(arr)。
            // 1.使用Arrays.stream将int[]转换成IntStream。
            // 2.使用IntStream中的boxed()装箱。将IntStream转换成Stream<Integer>。
            // 3.使用Stream的collect(),将Stream<T>转换成List<T>,因此正是List<Integer>。
    
            // int[] 转 Integer[]
            Integer[] integers1 = Arrays.stream(data).boxed().toArray(Integer[]::new);
            // 前两步同上,此时是Stream<Integer>。
            // 然后使用Stream的toArray,传入IntFunction<A[]> generator。
            // 这样就可以返回Integer数组。
            // 不然默认是Object[]。
    
            // List<Integer> 转 Integer[]
            Integer[] integers2 = list1.toArray(new Integer[0]);
            //  调用toArray。传入参数T[] a。这种用法是目前推荐的。
            // List<String>转String[]也同理。
    
            // List<Integer> 转 int[]
            int[] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray();
            // 想要转换成int[]类型,就得先转成IntStream。
            // 这里就通过mapToInt()把Stream<Integer>调用Integer::valueOf来转成IntStream
            // 而IntStream中默认toArray()转成int[]。
    
            // Integer[] 转 int[]
            int[] arr2 = Arrays.stream(integers1).mapToInt(Integer::valueOf).toArray();
            // 思路同上。先将Integer[]转成Stream<Integer>,再转成IntStream。
    
            // Integer[] 转 List<Integer>
            List<Integer> list2 = Arrays.asList(integers1);
            // 最简单的方式。String[]转List<String>也同理。
    
            // 同理
            String[] strings1 = {"a", "b", "c"};
            // String[] 转 List<String>
            List<String> list3 = Arrays.asList(strings1);
            // List<String> 转 String[]
            String[] strings2 = list3.toArray(new String[0]);
    

    镜像地址

    http://www.zhangwei.wiki/#/posts/8

    pay

  • 相关阅读:
    JS-字符串截取方法slice、substring、substr的区别
    Vue中computed和watch的区别
    Vue响应式原理及总结
    JS实现深浅拷贝
    JS中new操作符源码实现
    点击页面出现爱心效果
    js判断对象是否为空对象的几种方法
    深入浅出js实现继承的7种方式
    es6-class
    详解 ESLint 规则,规范你的代码
  • 原文地址:https://www.cnblogs.com/coderzhw/p/11094326.html
Copyright © 2011-2022 走看看