In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the comparative analysis of the efficiency of for and foreach, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
/ / for loop public static void main (String [] args) {String [] strs = {"3333", "2222", "1111"}; List list = Arrays.asList (strs); list = new ArrayList (list); for (int I = 0; I
< list.size(); i++){ String str = list.get(i); if (i == 2){ list.remove(str); continue; } System.out.println(str); } } //foreach循环 public static void main(String[] args) { String[] strs = {"3333", "2222", "1111"}; List list = Arrays.asList(strs); list = new ArrayList(list); for (String str: list){ if (str.equals("1111")){ //异常抛出点 list.remove(str); continue; } System.out.println(str); } } 先从原理上进行分析,for循环没啥好说的,主要是foreach,foreach的源码如下: //使用迭代器的next方法遍历 for (Iterator localIterator = list.iterator(); localIterator.hasNext(); ) { //do something... } 由于foreach是使用的迭代器的方式遍历的,所以在对最后一个数据元素进行remove操作时,会抛出异常。 for循环运行:The foreach loop runs:
The efficiency of the two is poor:
Using ArrayList for random access is faster, while the get () method in the for loop uses the random access method, so in ArrayList, the for loop is faster.
Using LinkedList is faster for sequential access, and the next () method in iterator uses the sequential access method, so it is faster to use iterator in LinkedList.
Code:
/ / for cycle and foreach efficiency comparison public static void main (String [] args) {/ / LinkedList run List list = new LinkedList (); / / ArrayList run / / List list = new ArrayList (); for (int I = 0; I
< 100000; i++){ list.add(i); } long startTime=System.currentTimeMillis(); //获取开始时间 for (int i = 0; i < list.size(); i++){ //不输出数据,程序运行时间太短 System.out.println(list.get(i)); } long endTime=System.currentTimeMillis(); //获取结束时间 long fortime = endTime - startTime; startTime=System.currentTimeMillis(); //获取开始时间 for (int i : list){ //不输出数据,程序运行时间太短 System.out.println(i); } endTime=System.currentTimeMillis(); //获取结束时间 System.out.println("for运行时间: "+fortime+"ms"); System.out.println("foreach运行时间: "+(endTime - startTime)+"ms"); } LinkedList运行结果: ArrayList运行结果:The above is the comparative analysis of the efficiency of for and foreach. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 295
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.