• java 三个循环的优缺点


     1 package cc.knms.appservice.test;
     2 
     3 import java.text.ParseException;
     4 import java.util.ArrayList;
     5 import java.util.Iterator;
     6 import java.util.List;
     7 
     8 public class Test {
     9     /**
    10      * @autho 方华哥哥
    11      * @remark 关于list 性能 ,删除其中的元素测试
    12      *     //附带说下这三个循环的性能
    13      * @date 2016年12月21日 下午1:48:33
    14      * @param
    15      * @return
    16      * @throws ParseException
    17      */
    18     public static void main(String[] args) throws ParseException {
    19         // 循环中 删除元素测试
    20         List<String> list = new ArrayList<String>();
    21         list.add("a");
    22         list.add("b");
    23         list.add("c");
    24         list.add("d");
    25         list.add("e");
    26         
    27         
    28         for (int i = 0; i < list.size(); i++) {
    29             if (list.get(i).equals("b")) {
    30                 list.remove(i);
    31             }
    32             System.out.println(list.get(i));
    33             System.out.println(list + "--");
    34         }
    35         //性能:内部不锁定,效率最高,但是当写多线程时要考虑并发操作的问题
    36         /* 内部不锁定也是其缺点,这样会导致循环当中删除某个元素可能会出现不必要的问题
    37          * 通过结果我们可以看到,在基本的for循环中 我们也能正常的删除元素,但是这里的指针会向前移动. a b-- c c-- d c-- e
    38          * c--
    39          */
    40         
    41         
    42     
    43         for (String string : list) {
    44             if (string.equals("b")) {
    45                 list.remove(string);
    46             }
    47             System.out.println(string);
    48         }
    49         // 性能:当遍历集合或数组时,如果需要访问集合或数组的下标,
    50         //     那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息其实
    51 
    52         /*
    53          * 在增强for循环中删除元素我们可以看到直接报错 aException in thread "main" b
    54          * java.util.ConcurrentModificationException
    55          */
    56         
    57         
    58         Iterator<String> it = list.iterator();
    59         while (it.hasNext()) {
    60             if (it.next().equals("b")) {
    61                 it.remove();
    62             }
    63             System.out.println(list);
    64         }
    65         //性能:执行过程中会进行数据锁定,性能稍差,但是同时,如果你想在循环过程中去掉某个元素,为了避免不必要的问尽量使用it.remove方法;
    66         /*
    67          * Iterator 也能正常的删除,在循环中需要删除数据,然后在获取某个值的时候建议用Iterator,
    68          * 如果用基本的for循环的时候,可能得出的值会错乱,for循环每删除一个元素,指针会像前移动,如果通过get去获取值的时候可能不是自己想要的 
    69          * Iterator 则不会
    70          *  [a, b, c, d, e] [a, c, d, e] [a, c, d, e] [a,
    71          * c, d, e] [a, c, d, e]
    72          */
    73     }
    74 
    75 }
  • 相关阅读:
    Python pip – error: invalid command ‘bdist_wheel’
    css实现文字两端对齐(兼容所有浏览器)
    webpack中的require.context
    node 的path模块中 path.resolve()和path.join()的区别
    react-native项目中遇到的问题
    react-native针对android改变状态栏样式
    createBottomTabNavigator: 怎么在切换tab的时候让页面重新渲染
    当vue页面异步加载的数据想在页面上渲染怎么办
    git分布式版本控制系统
    $router和$route的区别,路由跳转方式name 、 path 和传参方式params 、query的区别
  • 原文地址:https://www.cnblogs.com/fanghuagege/p/6207621.html
Copyright © 2020-2023  润新知