java速学教程(入门到精通)
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
for-each不是一种新语法,而是java的语法糖。在编译时,编译器将此代码转换为迭代器实现,并将其编译为字节码。我们可以通过执行命令javap-verbose-testforeach
反编译以下编译代码:
public class TestForeach { List<integer> integers; public void testForeach(){ for(Integer i : integers){ } } }</integer>
获得的详细字节码如下:
public void testForeach(); descriptor: ()V flags: ACC_PUBLIC Code: stack=1, locals=3, args_size=1 0: aload_0 1: getfield #2 // Field integers:Ljava/util/List; 4: invokeinterface #3, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 9: astore_1 10: aload_1 11: invokeinterface #4, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 16: ifeq 32 19: aload_1 20: invokeinterface #5, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 25: checkcast #6 // class java/lang/Integer 28: astore_2 29: goto 10 32: return LineNumberTable: line 11: 0 line 13: 29 line 14: 32 LocalVariableTable: Start Length Slot Name Signature 29 0 2 i Ljava/lang/Integer; 0 33 0 this Ltest/TestForeach; }
此字节码的一般含义是使用getfileld
命令来获取integers
变量并且调用List.iterator
来获取迭代器实例和调用iterator.hasNext
。如果返回true
,调用iterator.next
方法。
请看,这是迭代器遍历集合的实现逻辑。
现在让我们使用for循环方法和for-each方法进行测试。
public class ForLoopTest { public static void main(String[] args) { List<integer> arrayList = new ArrayList(); for (int i = 0; i <p>这是测试结果:</p> <p><img src="https://img.php.cn/upload/article/000/465/014/168499777276747.png?x-oss-process=image/resize,p_40" alt="Java循环中的For和For-each应用对比分析"></p> <p>如你所见,结果是显而易见的。使用For循环方法比For each方法在ArrayList上表现更为高效。</p> <p>我们可以说for循环比for-each好吗?</p> <p>答案是否定的。在下一个基准测试中,我们将ArrayList更改为LinkedList。<br>同样,这里是测试结果。</p> <p><img src="https://img.php.cn/upload/article/000/465/014/168499777223357.png?x-oss-process=image/resize,p_40" alt="Java循环中的For和For-each应用对比分析"></p> <h3>原因分析</h3> <p>一些初学者可能想知道为什么ArrayList使用for循环方法遍历得更快,而LinkedList则更慢,速度也非常慢?</p> <p>这由ArrayList和LinkedList数据结构决定。<br>ArrayList底层使用数组存储元素。数组是连续的内存空间。数据可以通过索引获得。时间复杂度为O(1),因此速度很快。</p> <p>LinkedList的底层是一个双向链表。使用for循环实现遍历,每次都需要从链表的头节点开始。时间复杂度为O(n*n)。</p> <h3>结论</h3> <ul class=" list-paddingleft-2"> <li><p>使用ArrayList时,for循环方法更快,因为for-each由迭代器实现,并且需要执行并发修改验证。</p></li> <li><p>使用LinkedList时,for-each比for循环快得多,因为LinkedList是通过使用双向链表实现的。每个寻址都需要从头节点开始。遍历LinkedList时,避免使用for循环。</p></li> <li><p>使用迭代器模式,for-each不需要关心集合的具体实现。如果需要替换集合,无需修改代码即可轻松替换。</p></li> </ul></integer>
Java免费学习笔记:立即学习
解锁 Java 大师之旅:从入门到精通的终极指南
已抢7215个
抢已抢94863个
抢已抢14828个
抢已抢52105个
抢已抢194768个
抢已抢87281个
抢