[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-685691-105":3,"detail-sidebar-cat-0-en-105":80,"doc-detail-685691-en":130,"doc-similar-685691":154},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":73,"head_meta":75,"extra_data":77,"updated_unix":79},105,"en","arraylist-and-iterator-in-java-java-list-iteration-methods-and-examples","ArrayList and Iterator in Java - Java List Iteration Methods and Examples","","ArrayList operations and Java iteration techniques are explained with practical examples and method usage. The content covers how inserting elements into an ArrayList affects performance, then details core ArrayList methods such as add, remove, set, indexOf, get, size, contains, and clear. It further demonstrates iterating through a List using for loop, advanced for loop (enhanced for), Iterator with hasNext/next/remove, while loop, and Java 8 stream() forEach. Complete code samples show building lists, updating elements, printing results, and removing items.",{"@graph":14,"@context":72},[15,34,55],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/document/","Document",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/document/technology/","Technology",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/document/arraylist-and-iterator-in-java-java-list-iteration-methods-and-examples/685691/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":49,"encodingFormat":47,"isAccessibleForFree":50,"interactionStatistic":51},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/arraylist-and-iterator-in-java-java-list-iteration-methods-and-examples/685691.png","ImageObject",300,407,{"name":42,"@type":43},"vwvw","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-11-12","2026-11-07",true,{"@type":52,"interactionType":53,"userInteractionCount":33},"InteractionCounter",{"@type":54},"ViewAction",{"@type":56,"mainEntity":57},"FAQPage",[58,64,68],{"name":59,"@type":60,"acceptedAnswer":61},"Why is inserting elements between existing elements in an ArrayList inefficient?","Question",{"text":62,"@type":63},"Because elements after the insertion point must be shifted, which can be expensive as it may require moving many items in the collection.","Answer",{"name":65,"@type":60,"acceptedAnswer":66},"How do you update an element in an ArrayList?",{"text":67,"@type":63},"Use set(int index, Object o). It replaces the element at the specified index with the provided value.",{"name":69,"@type":60,"acceptedAnswer":70},"What are the main ways to iterate through a Java List?",{"text":71,"@type":63},"The document lists five ways: for loop, advanced (enhanced) for loop, Iterator, while loop, and Java 8 stream() forEach.","https://schema.org",{"og:url":32,"og:type":74,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":76,"canonical":32},"index,follow",{"doc_id":78,"site_id":7},685691,1794016767,{"code":4,"msg":81,"data":82},"success",[83,87,91,95,100,104,109,114,119,122,126],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":84,"show_sort_weight":85,"slug":86},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":88,"show_sort_weight":89,"slug":90},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":92,"show_sort_weight":93,"slug":94},"Exam",70,"exam",{"id":96,"doc_module":4,"doc_module_name":25,"category_name":97,"show_sort_weight":98,"slug":99},5,"Comic",60,"comic",{"id":101,"doc_module":4,"doc_module_name":25,"category_name":29,"show_sort_weight":102,"slug":103},6,50,"technology",{"id":105,"doc_module":4,"doc_module_name":25,"category_name":106,"show_sort_weight":107,"slug":108},7,"Healthcare",40,"healthcare",{"id":110,"doc_module":4,"doc_module_name":25,"category_name":111,"show_sort_weight":112,"slug":113},8,"Research & Report",30,"research-report",{"id":115,"doc_module":4,"doc_module_name":25,"category_name":116,"show_sort_weight":117,"slug":118},9,"Religion & Spirituality",20,"religion-spirituality",{"id":117,"doc_module":4,"doc_module_name":25,"category_name":120,"show_sort_weight":117,"slug":121},"World Cup","world-cup",{"id":123,"doc_module":4,"doc_module_name":25,"category_name":124,"show_sort_weight":123,"slug":125},10,"Lifestyle","lifestyle",{"id":127,"doc_module":4,"doc_module_name":25,"category_name":128,"show_sort_weight":96,"slug":129},19,"General","general",{"code":4,"msg":81,"data":131},{"doc_id":78,"user_id":132,"nickname":42,"user_avatar":133,"doc_module":4,"category_id":101,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":33,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":139,"language":140,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":12,"update_tm":79,"read_time":144,"preview_page_count":26,"preview_urls":145},3985747874149,"https://ap-avatar.wpscdn.com/avatar/3a001161165b4dc3d9e?x-image-process=image/resize,m_fixed,w_180,h_180&k=1790129441039064114","1. ArrayList and Iterator in Java  \nInserting elements between existing elements of an ArrayList or Vector is an inefficient operation-all element after the new one must be moved out of the way which could be an expensive operation in a collection.  \nTo define ArrayList  \nArrayList \u003CString> obj = new ArrayList \u003CString>();  \n􀁸 Methods of ArrayList class  \na. add( Object o): This method adds an object o to the arraylist.  \n obj.add (\"hello\");   \nb. add(int index, Object o): It adds the object o to the array list at the given index.  \n obj.add (2, \"bye\");   \nIt will add the string bye to the 2nd index (3rd position as the array list starts with index 0) of array list.  \nc. remove(Object o): Removes the object o from the ArrayList  \n obj.remove (\"Chaitanya\");   \nThis statement will remove the string “Chaitanya” from the ArrayList.  \nd. remove(int index): Removes element from a given index.  \nobj .remove (3);   \nIt would remove the element of index 3 (4th element of the list – List starts with o) .  \ne. set(int index, Object o): Used for updating an element. It replaces the element present at the specified index with the object o.  \nobj.set (2, \"Tom\");  \nIt would replace the 3rd element (index =2 is 3rd element) with the value Tom.  \n[f. int](f. int) indexOf(Object o): Gives the index of the object o. If the element is not found in the list then this method returns the value-1.  \nint pos = obj.indexOf (\"Tom\");  \nThis would give the index (position) of the string Tom in the list.  \ng. Object get(int index): It returns the object of list which is present at the specified index.  \nString str = obj.get (2);  \nFunction get would return the string stored at 3rd position (index 2) and would be assigned to the string “str”. We have stored the returned value in string variable because in our example we have defined the ArrayList is of String type. If you are having integer array list then the returned value should be stored in an integer variable.  \n[h.](h. int)[ int](h. int) size(): It gives the size of the ArrayList – Number of elements of the list.  \nint numberofitems = obj.size();  \ni. boolean contains(Object o): It checks whether the given object o is present in the array list if its there then it returns true else it returns false.  \n obj. contains (\"Steve\");   \nIt would return true if the string “Steve” is present in the list else we would get false.  \nj. clear(): It is used for removing all the elements of the array list in one go. The below code will remove all the elements of ArrayList whose object is obj.  \n obj. clear ();   \nExamples  \npackage com.tutorialspoint;  \nimport java.util.ArrayList;  \npublic class ArrayListDemo {  \npublic static void main(String[] args) {  \n// create an empty arraylist with an initial capacity  \nArrayList\u003CInteger> arrlist = new ArrayList\u003CInteger>(5);  \n// use add() method to add elements in the list  \narrlist.add(15);  \narrlist.add(20);  \narrlist.add(25);  \narrlist.add(22);  \n// let us print all the elements available in list  \nfor (Integer number : arrlist) {  \nSystem.out.println(\"Number = \" + number);}  \n// inserting elment 55 at 3rd position  \narrlist.set(2,55);  \n// let us print all the elements available in list  \nSystem.out.println(\"Printing new list:\"); for (Integer number : arrlist) {  \nSystem.out.println(\"Number = \" + number);}  \n}  \n}  \nLet us compile and run the above program, this will produce the following result:  \nNumber = 15  \nNumber = 20  \nNumber = 25  \nNumber = 22 Printing new list:  \nNumber = 15  \nNumber = 20  \nNumber = 55  \nNumber = 22  \nHow to iterate through Java List? This tutorial demonstrates the use of ArrayList, Iterator and a List.  \nThere are 5 ways you can iterate through List.  \n1. For Loop  \n2. Advanced For Loop  \n3. Iterator  \n4. While Loop  \n5. Collections’s stream() util (Java8)  \nimport java.util.List;  \nimport java.util.ArrayList;  \nimport java.util.Collection;  \nimport java.util.Iterator;  \npublic class CollectionTest {  \nprivate static final String[] colors =  \n{ ","cbCaifKdiTSosffR","https://ap.wps.com/l/cbCaifKdiTSosffR","pdf",1060216,17,"English","# ArrayList basics and performance\n## Creating an ArrayList\n## Core ArrayList methods (add, remove, set, indexOf, get, size, contains, clear)\n# Java List iteration methods\n## For loop\n## Enhanced for loop\n## Iterator (hasNext/next/remove)\n## While loop\n## Java 8 stream() forEach\n# Example programs","[{\"question\":\"Why is inserting elements between existing elements in an ArrayList inefficient?\",\"answer\":\"Because elements after the insertion point must be shifted, which can be expensive as it may require moving many items in the collection.\"},{\"question\":\"How do you update an element in an ArrayList?\",\"answer\":\"Use set(int index, Object o). It replaces the element at the specified index with the provided value.\"},{\"question\":\"What are the main ways to iterate through a Java List?\",\"answer\":\"The document lists five ways: for loop, advanced (enhanced) for loop, Iterator, while loop, and Java 8 stream() forEach.\"}]","ArrayList and Iterator in Java - Java List Iteration Methods and Examples | PDF",43,[146,150],{"page_no":22,"url":147,"width":148,"height":149},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/d052669b7d8cf259dfff472c68840b27.webp",983,1390,{"page_no":26,"url":151,"width":152,"height":153},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/a8854882ce76c6e98771af0a326ab62a.webp",982,1388,{"list":155,"token":11},[156,162,168,174,180,186,193,199,205,211],{"doc_id":157,"doc_title":158,"file_type":137,"home_preview_img_url":159,"slug":160,"view_count":96,"category_name":29,"create_tm":161},685686,"ArrayList in Java","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000179246/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=T4vno4SFMVZNKTIjwvrVM4sgg2U%3D","arraylist-in-java",1794016760,{"doc_id":163,"doc_title":164,"file_type":137,"home_preview_img_url":165,"slug":166,"view_count":33,"category_name":29,"create_tm":167},685658,"Lesson 4 - Collections and iterators","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000173197/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=Y5idnmpiECgNhFGI32XqYlcMiN8%3D","lesson-4-collections-and-iterators",1794016351,{"doc_id":169,"doc_title":170,"file_type":137,"home_preview_img_url":171,"slug":172,"view_count":101,"category_name":29,"create_tm":173},697916,"Java Collections - List Set Map","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010130865/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=OZ%2B9sR1zj0M7lsyVnokuN6u3uUY%3D","java-collections-list-set-map",1794445817,{"doc_id":175,"doc_title":176,"file_type":137,"home_preview_img_url":177,"slug":178,"view_count":96,"category_name":29,"create_tm":179},697921,"Java Collections Framework - Interfaces, Implementations, and Core Algorithms","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010131024/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=UJXcDm1dGqm%2Fe4uK2u6i70hN4Mw%3D","java-collections-framework-interfaces-implementations-and-core-algorithms",1794445828,{"doc_id":181,"doc_title":182,"file_type":137,"home_preview_img_url":183,"slug":184,"view_count":33,"category_name":29,"create_tm":185},685689,"List and ArrayList - Java Data Structure Overview","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000179285/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=BDQfdrK5jX%2FzX4hvjlHbgSfBdQI%3D","list-and-arraylist-java-data-structure-overview",1794016763,{"doc_id":187,"doc_title":188,"file_type":137,"home_preview_img_url":189,"slug":190,"view_count":191,"category_name":29,"create_tm":192},311712,"AP Computer Science A - Java Quick Reference - Exam Reference Information","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229907590499/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=BB2Y3hQYCqVo2qi35FocFXRCZVs%3D","ap-computer-science-a-java-quick-reference-exam-reference-information",14,1789894691,{"doc_id":194,"doc_title":195,"file_type":137,"home_preview_img_url":196,"slug":197,"view_count":96,"category_name":29,"create_tm":198},685694,"Implementing an ArrayList - Part 1 - Homework/Exam Announcements","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000179430/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=5WsAMXhhFu7BTmjhoaK0vMoRqX0%3D","implementing-an-arraylist-part-1-homeworkexam-announcements",1794016774,{"doc_id":200,"doc_title":201,"file_type":137,"home_preview_img_url":202,"slug":203,"view_count":123,"category_name":29,"create_tm":204},685794,"Core Java - Volume I - Fundamentals - Twelfth Edition","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000212435/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=rwgdm9aNFzcXduvZW%2F88veweD2E%3D","core-java-volume-i-fundamentals-twelfth-edition",1794018671,{"doc_id":206,"doc_title":207,"file_type":137,"home_preview_img_url":208,"slug":209,"view_count":30,"category_name":29,"create_tm":210},685796,"ArrayLists - Introduction to Java ArrayList and Generics","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000212466/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=kYLoeUGSbyDFu%2FGmpgtOpBtFtTw%3D","arraylists-introduction-to-java-arraylist-and-generics",1794018675,{"doc_id":212,"doc_title":213,"file_type":137,"home_preview_img_url":214,"slug":215,"view_count":101,"category_name":29,"create_tm":216},685799,"ARRAY LISTS - Lecture 17 - CS302 Introduction to Programming","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000212652/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111939&Signature=uywwJmT8oQuT67VaO%2F8qlRJgd9E%3D","array-lists-lecture-17-cs302-introduction-to-programming",1794018682]