[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-685686-105":3,"detail-sidebar-cat-0-en-105":81,"doc-detail-685686-en":130,"doc-similar-685686":149},{"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":74,"head_meta":76,"extra_data":78,"updated_unix":80},105,"en","arraylist-in-java","ArrayList in Java","","ArrayList in Java explains how the List collection stores elements in a dynamically resizable internal array, growing by allocating a new array and copying elements when capacity is exceeded. It compares ArrayList with arrays and Vector, noting advantages like automatic resizing and guidance for thread safety and synchronization. The material covers constructors, adding elements with add/addAll (including indexed insertion), updating via set, retrieving with get and indexOf/lastIndexOf, traversing with for, for-each, Iterator, and ListIterator, sorting, and removing or retaining elements using clear/remove/removeAll/retainAll.",{"@graph":14,"@context":73},[15,34,56],{"@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-in-java/685686/",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-in-java/685686.png","ImageObject",300,407,{"name":42,"@type":43},"dbqoiu","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-11-18","2026-11-07",true,{"@type":52,"interactionType":53,"userInteractionCount":55},"InteractionCounter",{"@type":54},"ViewAction",5,{"@type":57,"mainEntity":58},"FAQPage",[59,65,69],{"name":60,"@type":61,"acceptedAnswer":62},"How does ArrayList differ from arrays in size handling?","Question",{"text":63,"@type":64},"ArrayList dynamically resizes as elements increase, while arrays have a fixed size. When capacity is reached, ArrayList creates a new array and copies existing elements.","Answer",{"name":66,"@type":61,"acceptedAnswer":67},"What are the main constructors of ArrayList?",{"text":68,"@type":64},"ArrayList() creates storage with initial size 10, ArrayList(int initialCapacity) sets the initial capacity, and ArrayList(Collection) builds a list from the specified collection.",{"name":70,"@type":61,"acceptedAnswer":71},"Which methods are used to modify and access elements in an ArrayList?",{"text":72,"@type":64},"Use set(index, value) to change an element. Use get(index) to retrieve an element, and indexOf/lastIndexOf to find the first or last occurrence of a value.","https://schema.org",{"og:url":32,"og:type":75,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":77,"canonical":32},"index,follow",{"doc_id":79,"site_id":7},685686,1794016760,{"code":4,"msg":82,"data":83},"success",[84,88,92,96,100,104,109,114,119,122,126],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":85,"show_sort_weight":86,"slug":87},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":89,"show_sort_weight":90,"slug":91},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":93,"show_sort_weight":94,"slug":95},"Exam",70,"exam",{"id":55,"doc_module":4,"doc_module_name":25,"category_name":97,"show_sort_weight":98,"slug":99},"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":55,"slug":129},19,"General","general",{"code":4,"msg":82,"data":131},{"doc_id":79,"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":55,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":55,"language":139,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":140,"faqs":141,"seo_title":142,"seo_description":12,"update_tm":80,"read_time":143,"preview_page_count":22,"preview_urls":144},3985747862462,"https://ap-avatar.wpscdn.com/davatar_3d24733baf745e90a7e4bdd5f77d97b2","ARRAYLIST IN JAVA  \nArrayList is part of the collections framework. ArrayList is a List and implements the java. util.list interface.  \nArrayList is a better alternative to Arrays, especially if you are not sure about the array size. Unlike array which have a fixed size, ArrayList can grow in size when needed.  \nInternally ArrayList also uses arrays to store data. When it reaches the current capacity and needs to grow, a new array is created and elements are copied from the old array to the new array. In the newer code, ArrayList is used along with generics.  \nArrayList has many advantages over arrays such as:  \n􀁴 It dynamically resizes based on the number of elements in the list.  \n􀁴 It reduces the memory footprint. In case of arrays, we need to allocate memory upfront as we cannot change later. So extra memory is allocated even if we don't use it.  \nIf you are sure about the number of elements or if primitive data types are the elements we can use Arrays instead.  \nThe ArrayList class is similar to a Vector as both can grow in size as needed. But unlike Vector, ArrayList is not synchronized. ArrayList is always preferred over a Vector. There are many ways through which we can make an ArrayList thread safe like Collections.synchronizedList(theArrayList) or we can use a use a CopyOnWriteArrayList.  \nIf simultaneous overwrite occurs in an ArrayList, a ConcurrentModificationException exception is thrown. Synchronizing a class when not required can affect the performance.  \nCreating ArrayList  \nThere are three constructors for creating an ArrayList.  \nWhen you create an ArrayList using the constructor ArrayList() , the internal array for storage is created with a size 10.  \nThe constructor ArrayList(int initialCapacity) allows us to set the initial capacity.  \nThe constructor ArrayList( Collection) constructs a list containing the elements of the specified collection.  \nIn the newer code, ArrayList is used along with generics:  \nArrayList\u003CString> list = new ArrayList\u003CString>();  \nArrayList\u003CString> list = new ArrayList\u003CString>(20);  \nArrayList\u003CString> list = new ArrayList\u003CString>(myCollection);  \nAdding elements  \nWe can add elements to an array list using the add or addAll methods that appends one or more elements to the end of the list, or an overloaded add or addAll with an index argument that inserts one or more elements at a position within the list.  \nArrayList\u003CString> names = new ArrayList\u003CString>();  \nnames.add(\"Heartin\");  \nnames.add(\"Sneha\");  \nnames.add(1, \"Jacob\");  \nSystem.out.println(names);  \nArrayList\u003CString> namesNew = new ArrayList\u003CString>();  \nnamesNew.addAll(names);  \nSystem.out.println(namesNew);  \nnamesNew.addAll(2,names);  \nSystem.out.println(namesNew);  \nThis will print:  \n[Heartin, Jacob, Sneha]  \n[Heartin, Jacob, Sneha]  \n[Heartin, Jacob, Heartin, Jacob, Sneha, Sneha]  \nChanging elements  \nWe can modify an element of a list using the set method.  \nnames.set(1, \"June\");  \nRetrieving elements and index  \nTo retrieve an element at a given position, use the get method.  \nSystem.out.println(names.get(2));  \nThe index of the first occurrence of an element can be obtained using the indexOf method.  \nSystem.out.println(names.indexOf(\"Jacob\"));  \n-1 is returned if the element is not present. The index of the last occurrence of an element can be obtained using lastIndexOf method.  \nTraversing  \nWe can traverse an ArrayList using:  \n􀁴 Simple for statement  \n􀁴 for-each statement  \n􀁴 Iterator  \n􀁴 ListIterator  \nExample -for each loop  \nfor(String str : names)  \n{  \nSystem.out.println(str);  \n}  \nExample – for loop  \nfor(int i=0; i\u003Cnames.size();i++)  \n{  \nSystem.out.println(names.get(i));  \n}  \nExample – Iterator  \nIterator\u003CString> iterator = names. iterator();  \nwhile (iterator. hasNext()) {  \nSystem.out. println([iterator.next](iterator.next)());  \n}  \nExample – ListIterator  \nListIterator can be used to traverse the list in both directions.  \nListIterator\u003CString> listIterator = names. listIterator();  \nwhile (listIt","cbCaikUJBYRzHYtZ","https://ap.wps.com/l/cbCaikUJBYRzHYtZ","pdf",156630,"English","# ArrayList in Java\n## Core concept and comparison\n## Creating an ArrayList\n## Adding, changing, and retrieving elements\n## Traversing the list\n## Sorting and removing elements","[{\"question\":\"How does ArrayList differ from arrays in size handling?\",\"answer\":\"ArrayList dynamically resizes as elements increase, while arrays have a fixed size. When capacity is reached, ArrayList creates a new array and copies existing elements.\"},{\"question\":\"What are the main constructors of ArrayList?\",\"answer\":\"ArrayList() creates storage with initial size 10, ArrayList(int initialCapacity) sets the initial capacity, and ArrayList(Collection) builds a list from the specified collection.\"},{\"question\":\"Which methods are used to modify and access elements in an ArrayList?\",\"answer\":\"Use set(index, value) to change an element. Use get(index) to retrieve an element, and indexOf/lastIndexOf to find the first or last occurrence of a value.\"}]","ArrayList in Java | PDF",13,[145],{"page_no":22,"url":146,"width":147,"height":148},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/5bd1f4ff2f4737e013bb659f027541e8.webp",983,1272,{"list":150,"token":11},[151,157,163,169,175,181,187,193,199,205],{"doc_id":152,"doc_title":153,"file_type":137,"home_preview_img_url":154,"slug":155,"view_count":30,"category_name":29,"create_tm":156},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=1796110859&Signature=afzFfUNRTcKpp5i3OgOt5yhe7uM%3D","arraylists-introduction-to-java-arraylist-and-generics",1794018675,{"doc_id":158,"doc_title":159,"file_type":137,"home_preview_img_url":160,"slug":161,"view_count":33,"category_name":29,"create_tm":162},685691,"ArrayList and Iterator in Java - Java List Iteration Methods and Examples","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000179348/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110859&Signature=A2Jtc8zaEv3SHVH5DSgbhwMerSM%3D","arraylist-and-iterator-in-java-java-list-iteration-methods-and-examples",1794016767,{"doc_id":164,"doc_title":165,"file_type":137,"home_preview_img_url":166,"slug":167,"view_count":33,"category_name":29,"create_tm":168},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=1796110859&Signature=Zu%2B8lCD7Xg68%2BF0oqMwWrnprcyo%3D","list-and-arraylist-java-data-structure-overview",1794016763,{"doc_id":170,"doc_title":171,"file_type":137,"home_preview_img_url":172,"slug":173,"view_count":33,"category_name":29,"create_tm":174},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=1796110859&Signature=gigWy6r%2FxnFMwQfHwortEqwtYHA%3D","lesson-4-collections-and-iterators",1794016351,{"doc_id":176,"doc_title":177,"file_type":137,"home_preview_img_url":178,"slug":179,"view_count":55,"category_name":29,"create_tm":180},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=1796110859&Signature=2WtCPJnSSxBR6OPwiNKWG1uSHqY%3D","java-collections-framework-interfaces-implementations-and-core-algorithms",1794445828,{"doc_id":182,"doc_title":183,"file_type":137,"home_preview_img_url":184,"slug":185,"view_count":55,"category_name":29,"create_tm":186},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=1796110859&Signature=bp4MO1TQtsNZ4o%2F9SX993pHI%2FV8%3D","implementing-an-arraylist-part-1-homeworkexam-announcements",1794016774,{"doc_id":188,"doc_title":189,"file_type":137,"home_preview_img_url":190,"slug":191,"view_count":115,"category_name":29,"create_tm":192},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=1796110859&Signature=N8yFWckLvjMwLA0gtSSlAhc8a2g%3D","core-java-volume-i-fundamentals-twelfth-edition",1794018671,{"doc_id":194,"doc_title":195,"file_type":137,"home_preview_img_url":196,"slug":197,"view_count":33,"category_name":29,"create_tm":198},685664,"General C# Cheat Sheet - Quick reference guide","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000173279/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110859&Signature=MHSlHrzB3HbcMoWZ1N5BUIaW%2Fpo%3D","general-c-cheat-sheet-quick-reference-guide",1794016366,{"doc_id":200,"doc_title":201,"file_type":137,"home_preview_img_url":202,"slug":203,"view_count":33,"category_name":29,"create_tm":204},685785,"Arrays and Array Lists - Enhanced for Loop and 2D Arrays","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000208921/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110859&Signature=N2cvZz91QnITg75Fa2NBgBMp5%2F0%3D","arrays-and-array-lists-enhanced-for-loop-and-2d-arrays",1794018474,{"doc_id":206,"doc_title":207,"file_type":137,"home_preview_img_url":208,"slug":209,"view_count":33,"category_name":29,"create_tm":210},697965,"Arrays Made Easy - An Introduction to Arrays and Array Processing - SUGI 30 Tutorials","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010146275/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110859&Signature=vsLldAzxcyTCwEu%2BGnASZJXnFxQ%3D","arrays-made-easy-an-introduction-to-arrays-and-array-processing-sugi-30-tutorials",1794446535]