{"id":7811,"date":"2013-12-30T12:00:59","date_gmt":"2013-12-30T10:00:59","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=7811"},"modified":"2022-07-05T23:28:54","modified_gmt":"2022-07-05T20:28:54","slug":"arraylist-in-java-example-how-to-use-arraylist","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/","title":{"rendered":"Java ArrayList Example &#8211; How to use ArrayList (with video)"},"content":{"rendered":"<p>In this example, we will show how to use <a aria-label=\"ArrayList  (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/ArrayList.html\" target=\"_blank\">ArrayList<\/a> in Java. <\/p>\n<p>The class java.util.ArrayList provides a resizable array, which means that items can be added and removed from the list by using the provided ArrayList methods. It implements the <code><a aria-label=\"List  (opens in a new tab)\" rel=\"noreferrer noopener\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/List.html\" target=\"_blank\">List<\/a><\/code> interface.<\/p>\n<p>A major question related to arraylists is about when to use them instead of arrays and vice versa. An <code>ArrayList<\/code> is a dynamic data structure so it can be used when there is no upper bound on the number of elements.<\/p>\n<p>From the other side, a simple <code>Array<\/code> in java is a static data structure, because the initial size of an array cannot be changed, so it can be used only when the data has a known number of elements.<\/p>\n<p>You can also check this tutorial in the following video: <\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/ArrayList-Java-Example.jpg\"><img decoding=\"async\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/ArrayList-Java-Example-1024x576.jpg\" alt=\"\" class=\"wp-image-113891\" width=\"512\" height=\"288\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/ArrayList-Java-Example-1024x576.jpg 1024w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/ArrayList-Java-Example-300x169.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/ArrayList-Java-Example-768x432.jpg 768w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/07\/ArrayList-Java-Example.jpg 1280w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><figcaption> ArrayList Java Example &#8211; How to use ArrayList  &#8211; Video<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-arraylist-java-constructors\">1. ArrayList Java Constructors<\/h2>\n<p>The<code> ArrayList<\/code> class supports three constructors.<\/p>\n<ul class=\"wp-block-list\">\n<li><code>Arraylist()<\/code>This constructor builds an empty list.<\/li>\n<li><code>ArrayList(Collection&lt;? extends E&gt; c)<\/code>This constructor creates a list containing the elements of the specified collection. Note that E is the notation for the type of an element in a collection.<\/li>\n<li><code>ArrayList(int initialCapacity)<\/code>This constructor creates an empty list with the specified initial capacity.<\/li>\n<\/ul>\n<p>For example, if you want to create an empty array list of Strings then you would do the following:<\/p>\n<p><code>ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); <\/code><\/p>\n<p>If you want to create an array list with initial capacity, then you should do the following:<\/p>\n<p><code>ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(7);<\/code><\/p>\n<p><strong>Note<\/strong>: <code>ArrayList<\/code> class supports only object types and not primitive types.<\/p>\n<h2 class=\"wp-block-heading\" id=\"h-2-arraylist-methods\">2. ArrayList methods<\/h2>\n<p>Here are some of the most useful ArrayList methods:<\/p>\n<ul class=\"wp-block-list\">\n<li><strong>Adding elements to the list<\/strong>\n<ul>\n<li><code>boolean add(Element e)<\/code>Adds the specified element to the end of this list.<\/li>\n<li><code>void add(int index, Element e)<\/code>Adds the specified element at the specified position in the list.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Removing elements from the list<\/strong>\n<ul>\n<li><code>void clear()<\/code>Removes all the elements from the list.<\/li>\n<li><code>E remove(int index)<\/code>Removes the element at the specified position in the list.<\/li>\n<li><code>protected void removeRange(int start, int end)<\/code>Removes from the list all the elements starting from index <code>start<\/code> (included) until index <code>end<\/code> (not included).<\/li>\n<\/ul>\n<\/li>\n<li><strong>Getting elements from the list<\/strong>\n<ul>\n<li><code>E get(int index)<\/code>Returns the element at the specified position.<\/li>\n<li><code>Object[] toArray()<\/code>Returns an array containing all the elements of the list in proper sequence.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Setting an element<\/strong>\n<ul>\n<li><code>E set(int index, E element)<\/code>Replaces the element at the specified position with the specified element.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Searching elements<\/strong>\n<ul>\n<li><code>boolean contains(Object o)<\/code>Returns true if the specified element is found in the list.<\/li>\n<li><code>int indexOf(Object o)<\/code>Returns the index of the first occurrence of the specified element in the list. If this element is not in the list, the method returns -1.<\/li>\n<li><code>int lastIndexOf(Object o)<\/code>Returns the index of the last occurrence of the specified element in the list. If this element is not in the list, the method returns -1.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Iterating the arraylist<\/strong>\n<ul>\n<li><code>Iterator iterator()<\/code>Returns an iterator over the elements in the list.<\/li>\n<li><code>ListIterator listIterator()<\/code>Returns a list iterator over the elements in this list.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Checking if the list is empty<\/strong>\n<ul>\n<li><code>boolean isEmpty()<\/code>Returns true if the list does not contain any element.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Getting the size of the list<\/strong>\n<ul>\n<li><code>int size()<\/code>Returns the length of the list (the number of elements contained in the list).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Those were the most commonly used ArrayList methods. For further details for each method or for other methods that are not mentioned in this section, you can have a look at the <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/ArrayList.html\">official Java API.<\/a><\/p>\n<h2 class=\"wp-block-heading\" id=\"h-3-examples-of-using-arraylist-in-java\">3. Examples of using ArrayList in Java<\/h2>\n<p><a aria-label=\"ArrayList (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/ArrayList.html\" target=\"_blank\" rel=\"noreferrer noopener\">ArrayList<\/a>, <a aria-label=\"LinkedList (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/LinkedList.html\" target=\"_blank\" rel=\"noreferrer noopener\">LinkedList<\/a>, <a aria-label=\"Vector (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/Vector.html\" target=\"_blank\" rel=\"noreferrer noopener\">Vector<\/a>, and <a aria-label=\"Stack  (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/Stack.html\" target=\"_blank\" rel=\"noreferrer noopener\">Stack<\/a> implement the <a aria-label=\"List  (opens in a new tab)\" href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/11\/docs\/api\/java.base\/java\/util\/List.html\" target=\"_blank\" rel=\"noreferrer noopener\">List<\/a> interface. <\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"575\" height=\"658\" src=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/07\/arraylist.jpg\" alt=\"ArrayList Java\" class=\"wp-image-75121\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/07\/arraylist.jpg 575w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/07\/arraylist-262x300.jpg 262w\" sizes=\"(max-width: 575px) 100vw, 575px\" \/><figcaption>Figure 1 ArrayList<\/figcaption><\/figure>\n<\/div>\n<p>In this step, I will create five Junit test classes to show the usages of <code>ArrayList<\/code> as well as <code>LinkedList<\/code>, <code>Vector<\/code>, and <code>Stack<\/code>.<\/p>\n<p>I will include both <code>Junit<\/code> and <code>Logback<\/code> libraries in the <code>pom.xml<\/code> file.<\/p>\n<p><span style=\"text-decoration: underline\"><em>pom.xml<\/em><\/span>\n<\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\n\txmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n\txsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n\t&lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\t&lt;groupId&gt;jcg.zheng.demo&lt;\/groupId&gt;\n\t&lt;artifactId&gt;java-arraylist-demo&lt;\/artifactId&gt;\n\t&lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n\t&lt;build&gt;\n\t\t&lt;sourceDirectory&gt;src&lt;\/sourceDirectory&gt;\n\t\t&lt;plugins&gt;\n\t\t\t&lt;plugin&gt;\n\t\t\t\t&lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\n\t\t\t\t&lt;version&gt;3.8.0&lt;\/version&gt;\n\t\t\t\t&lt;configuration&gt;\n\t\t\t\t\t&lt;release&gt;11&lt;\/release&gt;\n\t\t\t\t&lt;\/configuration&gt;\n\t\t\t&lt;\/plugin&gt;\n\t\t&lt;\/plugins&gt;\n\t&lt;\/build&gt;\n\t&lt;dependencies&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;junit&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;junit&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;4.12&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;ch.qos.logback&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;logback-access&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;1.2.3&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;ch.qos.logback&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;logback-classic&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;1.2.3&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t\t&lt;dependency&gt;\n\t\t\t&lt;groupId&gt;ch.qos.logback&lt;\/groupId&gt;\n\t\t\t&lt;artifactId&gt;logback-core&lt;\/artifactId&gt;\n\t\t\t&lt;version&gt;1.2.3&lt;\/version&gt;\n\t\t&lt;\/dependency&gt;\n\t&lt;\/dependencies&gt;\n&lt;\/project&gt;<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-3-1-listbasetest\">3.1 ListBaseTest<\/h3>\n<p>Create a JUnit class named <code>ListBaseTest.java<\/code> with the following code:<\/p>\n<p><span style=\"text-decoration: underline\"><em>ListBaseTest.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertFalse;\nimport static org.junit.Assert.assertTrue;\n\nimport java.util.Arrays;\nimport java.util.Iterator;\nimport java.util.List;\n\nimport org.junit.FixMethodOrder;\nimport org.junit.Test;\nimport org.junit.runners.MethodSorters;\nimport org.slf4j.Logger;\n\n@FixMethodOrder(MethodSorters.NAME_ASCENDING)\npublic abstract class ListBaseTest {\n\n\tprotected Logger logger;\n\n\tList&lt;String&gt; list;\n\n\t@Test\n\tpublic void test_add() {\n\t\t\/\/ Adding items to arrayList\n\t\tlist.add(\"new Item\");\n\t\tassertEquals(5, list.size());\n\n\t\t\/\/ Display the contents of the array list\n\t\tlogger.info(\"The arraylist contains the following elements: \" + list);\n\t}\n\n\t@Test\n\tpublic void test_contains() {\n\t\t\/\/ Checking if an element is included to the list\n\t\tassertTrue(list.contains(\"Item1\"));\n\t\tassertFalse(list.contains(\"Item5\"));\n\t}\n\n\t@Test\n\tpublic void test_get() {\n\t\tassertEquals(\"Item1\", list.get(0));\n\t\tassertEquals(\"Item2\", list.get(1));\n\t\tassertEquals(\"Item3\", list.get(2));\n\t\tassertEquals(\"Item4\", list.get(3));\n\t}\n\n\t@Test\n\tpublic void test_getSize() {\n\t\t\/\/ Getting the size of the list\n\t\tassertEquals(4, list.size());\n\t}\n\n\t@Test\n\tpublic void test_indexOf() {\n\t\t\/\/ Checking index of an item\n\t\tint pos = list.indexOf(\"Item2\");\n\t\tassertEquals(1, pos);\n\t}\n\n\t@Test\n\tpublic void test_isEmpty() {\n\t\t\/\/ Checking if array list is empty\n\t\tassertFalse(list.isEmpty());\n\t}\n\n\t@Test\n\tpublic void test_loop_arraylist_via_for() {\n\t\t\/\/ Retrieve elements from the arraylist via foreach\n\t\tlogger.info(\"Retrieving items using foreach loop\");\n\t\tfor (String str : list) {\n\t\t\tlogger.info(\"Item is: \" + str);\n\t\t}\n\t}\n\n\t@Test\n\tpublic void test_loop_arraylist_via_for_2() {\n\t\t\/\/ Retrieve elements from the arraylist via loop using index and size list\n\t\tlogger.info(\"Retrieving items with loop using index and size list\");\n\t\tfor (int i = 0; i &lt; list.size(); i++) {\n\t\t\tlogger.info(\"Index: \" + i + \" - Item: \" + list.get(i));\n\t\t}\n\t}\n\n\t@Test\n\tpublic void test_loop_arrayList_via_foreach() {\n\t\tlogger.info(\"Retrieving items using Java 8 Stream\");\n\t\tlist.forEach((item) -&gt; {\n\t\t\tlogger.info(item);\n\t\t});\n\t}\n\n\t@Test\n\tpublic void test_loop_arraylist_via_iterator_for() {\n\t\t\/\/ hasNext(): returns true if there are more elements\n\t\t\/\/ next(): returns the next element\n\t\tlogger.info(\"Retrieving items using iterator\");\n\t\tfor (Iterator&lt;String&gt; it = list.iterator(); it.hasNext();) {\n\t\t\tlogger.info(\"Item is: \" + it.next());\n\t\t}\n\t}\n\n\t@Test\n\tpublic void test_loop_arraylist_via_iterator_while() {\n\t\tIterator&lt;String&gt; it = list.iterator();\n\t\tlogger.info(\"Retrieving items using iterator\");\n\t\twhile (it.hasNext()) {\n\t\t\tlogger.info(\"Item is: \" + it.next());\n\t\t}\n\t}\n\n\t@Test\n\tpublic void test_remove() {\n\t\t\/\/ removing the item in index 0\n\t\tlist.remove(0);\n\t\tassertEquals(3, list.size());\n\t\tassertEquals(\"Item2\", list.get(0));\n\t\tassertEquals(\"Item3\", list.get(1));\n\t\tassertEquals(\"Item4\", list.get(2));\n\n\t\t\/\/ removing the first occurrence of item \"Item3\"\n\t\tlist.remove(\"Item3\");\n\t\tassertEquals(2, list.size());\n\t\tassertEquals(\"Item2\", list.get(0));\n\t\tassertEquals(\"Item4\", list.get(1));\n\t}\n\n\t@Test\n\tpublic void test_replace() {\n\t\t\/\/ Replacing an element\n\t\tlist.set(1, \"NewItem\");\n\t\tassertEquals(\"Item1\", list.get(0));\n\t\tassertEquals(\"NewItem\", list.get(1));\n\t\tassertEquals(\"Item3\", list.get(2));\n\t\tassertEquals(\"Item4\", list.get(3));\n\t}\n\n\t@Test\n\tpublic void test_toArray() {\n\t\t\/\/ Converting ArrayList to Array\n\t\tString[] simpleArray = list.toArray(new String[list.size()]);\n\t\tlogger.info(\"The array created after the conversion of our arraylist is: \" + Arrays.toString(simpleArray));\n\t}\n}<\/pre>\n<p>In the above code, we can see that many <code>List<\/code> usage cases are covered. Adding elements to the list using two different methods, removing elements, getting the size of the list, checking if the list is empty, checking if a specific element is contained to the list. Also, five different ways are presented for retrieving the elements of a list. Finally, we show how to convert an <code>List<\/code> to <code>Array<\/code>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h3 class=\"wp-block-heading\" id=\"h-3-2-arraylisttest\">3.2 ArrayListTest<\/h3>\n<p>Create a JUnit class named <code>ArrayListTest.java<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>ArrayListTest.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertTrue;\n\nimport java.util.ArrayList;\n\nimport org.junit.Before;\nimport org.slf4j.LoggerFactory;\n\npublic class ArrayListTest extends ListBaseTest {\t\n\n\t@Before\n\tpublic void setup_list_with_4_items() {\n\t\tlogger = LoggerFactory.getLogger(this.getClass());\n\t\t\n\t\t\/\/ Creating an empty array list\n\t\tlist = new ArrayList&lt;String&gt;();\n\t\tassertTrue(list.isEmpty());\n\n\t\t\/\/ Adding items to arrayList\n\t\tlist.add(\"Item1\");\n\t\tassertEquals(1, list.size());\n\t\tlist.add(\"Item2\");\n\t\tassertEquals(2, list.size());\n\t\tlist.add(2, \"Item3\"); \/\/ add Item3 to the third position of array list\n\t\tassertEquals(3, list.size());\n\t\tlist.add(\"Item4\");\n\t\tassertEquals(4, list.size());\n\t}\n\n}\n<\/pre>\n<p>Execute <code>mvn test -Dtest=ArrayListTest<\/code> and capture output.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:bash\">Running jcg.zheng.demo.ArrayListTest\n16:01:02.294 [main] INFO jcg.zheng.demo.ArrayListTest - The arraylist contains the following elements: [Item1, Item2, Item3, Item4, new Item]\n16:01:02.308 [main] INFO jcg.zheng.demo.ArrayListTest - Retrieving items using Java 8 Stream\n16:01:02.310 [main] INFO jcg.zheng.demo.ArrayListTest - Item1\n16:01:02.310 [main] INFO jcg.zheng.demo.ArrayListTest - Item2\n16:01:02.310 [main] INFO jcg.zheng.demo.ArrayListTest - Item3\n16:01:02.310 [main] INFO jcg.zheng.demo.ArrayListTest - Item4\n16:01:02.311 [main] INFO jcg.zheng.demo.ArrayListTest - Retrieving items using foreach loop\n16:01:02.315 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item1\n16:01:02.315 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item2\n16:01:02.315 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item3\n16:01:02.315 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item4\n16:01:02.318 [main] INFO jcg.zheng.demo.ArrayListTest - Retrieving items with loop using index and size list\n16:01:02.352 [main] INFO jcg.zheng.demo.ArrayListTest - Index: 0 - Item: Item1\n16:01:02.352 [main] INFO jcg.zheng.demo.ArrayListTest - Index: 1 - Item: Item2\n16:01:02.352 [main] INFO jcg.zheng.demo.ArrayListTest - Index: 2 - Item: Item3\n16:01:02.352 [main] INFO jcg.zheng.demo.ArrayListTest - Index: 3 - Item: Item4\n16:01:02.353 [main] INFO jcg.zheng.demo.ArrayListTest - Retrieving items using iterator\n16:01:02.354 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item1\n16:01:02.354 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item2\n16:01:02.354 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item3\n16:01:02.354 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item4\n16:01:02.356 [main] INFO jcg.zheng.demo.ArrayListTest - Retrieving items using iterator\n16:01:02.357 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item1\n16:01:02.358 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item2\n16:01:02.358 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item3\n16:01:02.358 [main] INFO jcg.zheng.demo.ArrayListTest - Item is: Item4\n16:01:02.363 [main] INFO jcg.zheng.demo.ArrayListTest - The array created after the conversion of our arraylist is: [Item1, Item2, Item3, Item4]\nTests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.486 sec\n\nResults :\n\nTests run: 14, Failures: 0, Errors: 0, Skipped: 0\n\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n[INFO] Total time:  11.663 s\n[INFO] Finished at: 2019-07-26T16:01:02-05:00\n[INFO] ------------------------------------------------------------------------\n\nC:\\MaryZheng\\Workspaces\\jdk12\\java-arraylist-demo&gt;mvn test -Dtest=ArrayListTest<\/pre>\n<p>As we see in the output, the results are complied with what we described in the previous section. <\/p>\n<h3 class=\"wp-block-heading\" id=\"h-3-3-linkedlisttest\">3.3 LinkedListTest<\/h3>\n<p>Create a JUnit class named <code>LinkedListTest.java<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>LinkedListTest.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertTrue;\n\nimport java.util.LinkedList;\n\nimport org.junit.Before;\nimport org.slf4j.LoggerFactory;\n\npublic class LinkedListTest extends ListBaseTest {\n\n\t@Before\n\tpublic void setup_list_with_4_items() {\n\t\tlogger = LoggerFactory.getLogger(this.getClass());\n\t\t\n\t\t\/\/ Creating an empty linked list\n\t\tlist = new LinkedList&lt;String&gt;();\n\t\tassertTrue(list.isEmpty());\n\n\t\t\/\/ Adding items to arrayList\n\t\tlist.add(\"Item1\");\n\t\tassertEquals(1, list.size());\n\t\tlist.add(\"Item2\");\n\t\tassertEquals(2, list.size());\n\t\tlist.add(2, \"Item3\"); \/\/ add Item3 to the third position of array list\n\t\tassertEquals(3, list.size());\n\t\tlist.add(\"Item4\");\n\t\tassertEquals(4, list.size());\n\t}\n\n}\n<\/pre>\n<p>Execute <code>mvn test -Dtest=LinkedListTest<\/code> and capture output.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:bash\">Running jcg.zheng.demo.LinkedListTest\n16:03:20.954 [main] INFO jcg.zheng.demo.LinkedListTest - The arraylist contains the following elements: [Item1, Item2, Item3, Item4, new Item]\n16:03:20.967 [main] INFO jcg.zheng.demo.LinkedListTest - Retrieving items using Java 8 Stream\n16:03:20.968 [main] INFO jcg.zheng.demo.LinkedListTest - Item1\n16:03:20.968 [main] INFO jcg.zheng.demo.LinkedListTest - Item2\n16:03:20.968 [main] INFO jcg.zheng.demo.LinkedListTest - Item3\n16:03:20.969 [main] INFO jcg.zheng.demo.LinkedListTest - Item4\n16:03:20.969 [main] INFO jcg.zheng.demo.LinkedListTest - Retrieving items using foreach loop\n16:03:20.970 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item1\n16:03:20.971 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item2\n16:03:20.971 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item3\n16:03:20.971 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item4\n16:03:20.976 [main] INFO jcg.zheng.demo.LinkedListTest - Retrieving items with loop using index and size list\n16:03:21.015 [main] INFO jcg.zheng.demo.LinkedListTest - Index: 0 - Item: Item1\n16:03:21.015 [main] INFO jcg.zheng.demo.LinkedListTest - Index: 1 - Item: Item2\n16:03:21.015 [main] INFO jcg.zheng.demo.LinkedListTest - Index: 2 - Item: Item3\n16:03:21.016 [main] INFO jcg.zheng.demo.LinkedListTest - Index: 3 - Item: Item4\n16:03:21.017 [main] INFO jcg.zheng.demo.LinkedListTest - Retrieving items using iterator\n16:03:21.017 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item1\n16:03:21.018 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item2\n16:03:21.018 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item3\n16:03:21.018 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item4\n16:03:21.020 [main] INFO jcg.zheng.demo.LinkedListTest - Retrieving items using iterator\n16:03:21.022 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item1\n16:03:21.022 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item2\n16:03:21.022 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item3\n16:03:21.022 [main] INFO jcg.zheng.demo.LinkedListTest - Item is: Item4\n16:03:21.026 [main] INFO jcg.zheng.demo.LinkedListTest - The array created after the conversion of our arraylist is: [Item1, Item2, Item3, Item4]\nTests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.492 sec\n\nResults :\n\nTests run: 14, Failures: 0, Errors: 0, Skipped: 0\n\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n[INFO] Total time:  10.188 s\n[INFO] Finished at: 2019-07-26T16:03:21-05:00\n[INFO] ------------------------------------------------------------------------\n\nC:\\MaryZheng\\Workspaces\\jdk12\\java-arraylist-demo&gt;mvn test -Dtest=LinkedListTest<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-3-4-vectortest\">3.4 VectorTest<\/h3>\n<p>Create a JUnit class named <code>VectorTest.java<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>VectorTest.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertTrue;\n\nimport java.util.Vector;\n\nimport org.junit.Before;\nimport org.slf4j.LoggerFactory;\n\npublic class VectorTest extends ListBaseTest {\n\n\t@Before\n\tpublic void setup_list_with_4_items() {\n\t\tlogger = LoggerFactory.getLogger(this.getClass());\n\t\t\n\t\t\/\/ Creating an empty vector\n\t\tlist = new Vector&lt;String&gt;();\n\t\tassertTrue(list.isEmpty());\n\n\t\t\/\/ Adding items to arrayList\n\t\tlist.add(\"Item1\");\n\t\tassertEquals(1, list.size());\n\t\tlist.add(\"Item2\");\n\t\tassertEquals(2, list.size());\n\t\tlist.add(2, \"Item3\"); \/\/ add Item3 to the third position of array list\n\t\tassertEquals(3, list.size());\n\t\tlist.add(\"Item4\");\n\t\tassertEquals(4, list.size());\n\t}\n\n}\n<\/pre>\n<p>Execute <code>mvn test -Dtest=VectorTest<\/code> and capture output.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:bash\">Running jcg.zheng.demo.VectorTest\n16:05:02.444 [main] INFO jcg.zheng.demo.VectorTest - The arraylist contains the following elements: [Item1, Item2, Item3, Item4, new Item]\n16:05:02.459 [main] INFO jcg.zheng.demo.VectorTest - Retrieving items using Java 8 Stream\n16:05:02.465 [main] INFO jcg.zheng.demo.VectorTest - Item1\n16:05:02.466 [main] INFO jcg.zheng.demo.VectorTest - Item2\n16:05:02.466 [main] INFO jcg.zheng.demo.VectorTest - Item3\n16:05:02.467 [main] INFO jcg.zheng.demo.VectorTest - Item4\n16:05:02.469 [main] INFO jcg.zheng.demo.VectorTest - Retrieving items using foreach loop\n16:05:02.470 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item1\n16:05:02.470 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item2\n16:05:02.470 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item3\n16:05:02.471 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item4\n16:05:02.477 [main] INFO jcg.zheng.demo.VectorTest - Retrieving items with loop using index and size list\n16:05:02.527 [main] INFO jcg.zheng.demo.VectorTest - Index: 0 - Item: Item1\n16:05:02.527 [main] INFO jcg.zheng.demo.VectorTest - Index: 1 - Item: Item2\n16:05:02.528 [main] INFO jcg.zheng.demo.VectorTest - Index: 2 - Item: Item3\n16:05:02.528 [main] INFO jcg.zheng.demo.VectorTest - Index: 3 - Item: Item4\n16:05:02.529 [main] INFO jcg.zheng.demo.VectorTest - Retrieving items using iterator\n16:05:02.530 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item1\n16:05:02.531 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item2\n16:05:02.531 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item3\n16:05:02.531 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item4\n16:05:02.532 [main] INFO jcg.zheng.demo.VectorTest - Retrieving items using iterator\n16:05:02.533 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item1\n16:05:02.534 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item2\n16:05:02.534 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item3\n16:05:02.534 [main] INFO jcg.zheng.demo.VectorTest - Item is: Item4\n16:05:02.537 [main] INFO jcg.zheng.demo.VectorTest - The array created after the conversion of our arraylist is: [Item1, Item2, Item3, Item4]\nTests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.632 sec\n\nResults :\n\nTests run: 14, Failures: 0, Errors: 0, Skipped: 0\n\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n[INFO] Total time:  10.325 s\n[INFO] Finished at: 2019-07-26T16:05:02-05:00\n[INFO] ------------------------------------------------------------------------\n\nC:\\MaryZheng\\Workspaces\\jdk12\\java-arraylist-demo&gt;mvn test -Dtest=VectorTest<\/pre>\n<h3 class=\"wp-block-heading\" id=\"h-3-5-stacktest\">3.5 StackTest<\/h3>\n<p>Create a JUnit class named <code>StackTest.java<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>StackTest.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package jcg.zheng.demo;\n\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertTrue;\n\nimport java.util.Stack;\n\nimport org.junit.Before;\nimport org.junit.Test;\nimport org.slf4j.LoggerFactory;\n\npublic class StackTest extends ListBaseTest {\n\n\t@Before\n\tpublic void setup_list_with_4_items() {\n\t\tlogger = LoggerFactory.getLogger(this.getClass());\n\n\t\t\/\/ Creating an empty vector\n\t\tlist = new Stack&lt;String&gt;();\n\t\tassertTrue(list.isEmpty());\n\n\t\t\/\/ Adding items to arrayList\n\t\tlist.add(\"Item1\");\n\t\tassertEquals(1, list.size());\n\t\tlist.add(\"Item2\");\n\t\tassertEquals(2, list.size());\n\t\tlist.add(2, \"Item3\"); \/\/ add Item3 to the third position of array list\n\t\tassertEquals(3, list.size());\n\t\tlist.add(\"Item4\");\n\t\tassertEquals(4, list.size());\n\t}\n\n\t@Test\n\tpublic void test_pop() {\n\t\tString item = ((Stack&lt;String&gt;) list).pop();\n\t\tassertEquals(\"Item4\", item);\n\t}\n\n\t@Test\n\tpublic void test_push() {\n\t\t((Stack&lt;String&gt;) list).push(\"newValue\");\n\t\tassertEquals(5, list.size());\n\t}\n\n}\n<\/pre>\n<p>Execute <code>mvn test -Dtest=StackTest<\/code> and capture output.<\/p>\n<p><span style=\"text-decoration: underline\"><em>Output<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:bash\">Running jcg.zheng.demo.StackTest\n16:06:05.112 [main] INFO jcg.zheng.demo.StackTest - The arraylist contains the following elements: [Item1, Item2, Item3, Item4, new Item]\n16:06:05.125 [main] INFO jcg.zheng.demo.StackTest - Retrieving items using Java 8 Stream\n16:06:05.127 [main] INFO jcg.zheng.demo.StackTest - Item1\n16:06:05.127 [main] INFO jcg.zheng.demo.StackTest - Item2\n16:06:05.127 [main] INFO jcg.zheng.demo.StackTest - Item3\n16:06:05.127 [main] INFO jcg.zheng.demo.StackTest - Item4\n16:06:05.128 [main] INFO jcg.zheng.demo.StackTest - Retrieving items using foreach loop\n16:06:05.129 [main] INFO jcg.zheng.demo.StackTest - Item is: Item1\n16:06:05.129 [main] INFO jcg.zheng.demo.StackTest - Item is: Item2\n16:06:05.129 [main] INFO jcg.zheng.demo.StackTest - Item is: Item3\n16:06:05.129 [main] INFO jcg.zheng.demo.StackTest - Item is: Item4\n16:06:05.130 [main] INFO jcg.zheng.demo.StackTest - Retrieving items with loop using index and size list\n16:06:05.185 [main] INFO jcg.zheng.demo.StackTest - Index: 0 - Item: Item1\n16:06:05.185 [main] INFO jcg.zheng.demo.StackTest - Index: 1 - Item: Item2\n16:06:05.186 [main] INFO jcg.zheng.demo.StackTest - Index: 2 - Item: Item3\n16:06:05.187 [main] INFO jcg.zheng.demo.StackTest - Index: 3 - Item: Item4\n16:06:05.191 [main] INFO jcg.zheng.demo.StackTest - Retrieving items using iterator\n16:06:05.195 [main] INFO jcg.zheng.demo.StackTest - Item is: Item1\n16:06:05.197 [main] INFO jcg.zheng.demo.StackTest - Item is: Item2\n16:06:05.198 [main] INFO jcg.zheng.demo.StackTest - Item is: Item3\n16:06:05.198 [main] INFO jcg.zheng.demo.StackTest - Item is: Item4\n16:06:05.200 [main] INFO jcg.zheng.demo.StackTest - Retrieving items using iterator\n16:06:05.201 [main] INFO jcg.zheng.demo.StackTest - Item is: Item1\n16:06:05.202 [main] INFO jcg.zheng.demo.StackTest - Item is: Item2\n16:06:05.202 [main] INFO jcg.zheng.demo.StackTest - Item is: Item3\n16:06:05.202 [main] INFO jcg.zheng.demo.StackTest - Item is: Item4\n16:06:05.213 [main] INFO jcg.zheng.demo.StackTest - The array created after the conversion of our arraylist is: [Item1, Item2, Item3, Item4]\nTests run: 16, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.612 sec\n\nResults :\n\nTests run: 16, Failures: 0, Errors: 0, Skipped: 0\n\n[INFO] ------------------------------------------------------------------------\n[INFO] BUILD SUCCESS\n[INFO] ------------------------------------------------------------------------\n[INFO] Total time:  10.466 s\n[INFO] Finished at: 2019-07-26T16:06:05-05:00\n[INFO] ------------------------------------------------------------------------\n\nC:\\MaryZheng\\Workspaces\\jdk12\\java-arraylist-demo&gt;mvn test -Dtest=StackTest<\/pre>\n<h2 class=\"wp-block-heading\" id=\"h-4-summary\">4. Summary<\/h2>\n<p><code>ArrayList<\/code> is part of Java&#8217;s collection framework and implements Java&#8217;s <code>List<\/code> interface. <code>ArrayList<\/code> is used to store a dynamically sized collection of elements. Vector and ArrayList both use Array internally as a data structure. The main difference is that <code>Vector<\/code>&#8216;s methods are synchronized and <code>ArrayList<\/code>&#8216;s methods are not synchronized. Click <a aria-label=\"here  (opens in a new tab)\" href=\"https:\/\/www.connect2java.com\/tutorials\/collections\/arraylistlinkedlistvector-and-stack\/\" target=\"_blank\" rel=\"noreferrer noopener\">here <\/a>for more details.<\/p>\n<p>The performance differences between <code>ArrayList<\/code> and <code>LinkedList<\/code> are as the following:<\/p>\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td>Performance<\/td>\n<td>remove(int idx)<\/td>\n<td>get(int idx)<\/td>\n<td>add(E ele) &nbsp;  <\/td>\n<td>Iterator.remove()<\/td>\n<td>Iterator.add(E ele)<\/td>\n<\/tr>\n<tr>\n<td>  ArrayList<\/td>\n<td>\n  O(n)&nbsp;\n  <\/td>\n<td>\n  O(1)\n  <\/td>\n<td>\n  O(1)\n  <\/td>\n<td>\n  O(n)\n  <\/td>\n<td>\n  O(n)\n  <\/td>\n<\/tr>\n<tr>\n<td>   LinkedList   <\/td>\n<td>\n  O(n)\n  <\/td>\n<td>\n  O(n)\n  <\/td>\n<td>\n  O(1) when index=0<br \/>\n  Else O(n)\n  <\/td>\n<td>\n  O(1)\n  <\/td>\n<td>\n  O(1)\n  <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2 class=\"wp-block-heading\" id=\"h-5-related-articles\">5. Related articles<\/h2>\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/java-collections-tutorial\/\">Java Collections Tutorial<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/java-list-example\/\">Java List Example<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/java-array-example\/\">Java Array \u2013 java.util.Arrays Example<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/linkedlist-java-example\/\">LinkedList Java Example<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"h-6-download-the-source-code\">6. Download the Source Code<\/h2>\n<p>This was an ArrayList example in Java.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>Download the source code here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2019\/07\/java-arraylist-demo.zip\"><strong>ArrayList Java Example \u2013 How to use ArrayList<\/strong><\/a><\/div>\n<p><strong>Last updated on Jan. 11th, 2022<\/strong><\/p>\n<\/p>\n<p class=\"has-text-align-center\"><strong>Don&#8217;t forget to check out our&nbsp;<a rel=\"noreferrer noopener\" href=\"http:\/\/academy.javacodegeeks.com\/\" target=\"_blank\">Academy premium site<\/a>&nbsp;for advanced Java training!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this example, we will show how to use ArrayList in Java. The class java.util.ArrayList provides a resizable array, which means that items can be added and removed from the list by using the provided ArrayList methods. It implements the List interface. A major question related to arraylists is about when to use them instead &hellip;<\/p>\n","protected":false},"author":9,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[263],"class_list":["post-7811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arraylist","tag-featured"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java ArrayList - How to use (with video) - Examples Java Code Geeks<\/title>\n<meta name=\"description\" content=\"The ArrayList class in Java supports only object types &amp; not primitive types. Its methods are mostly used to add, remove elements in a list.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java ArrayList - How to use (with video) - Examples Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"The ArrayList class in Java supports only object types &amp; not primitive types. Its methods are mostly used to add, remove elements in a list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-12-30T10:00:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-05T20:28:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Konstantina Dimtsa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Konstantina Dimtsa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/\"},\"author\":{\"name\":\"Konstantina Dimtsa\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/5fc06d4d50814931c15af68106832a56\"},\"headline\":\"Java ArrayList Example &#8211; How to use ArrayList (with video)\",\"datePublished\":\"2013-12-30T10:00:59+00:00\",\"dateModified\":\"2022-07-05T20:28:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/\"},\"wordCount\":881,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"featured\"],\"articleSection\":[\"ArrayList\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/\",\"name\":\"Java ArrayList - How to use (with video) - Examples Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2013-12-30T10:00:59+00:00\",\"dateModified\":\"2022-07-05T20:28:54+00:00\",\"description\":\"The ArrayList class in Java supports only object types & not primitive types. Its methods are mostly used to add, remove elements in a list.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"util\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"ArrayList\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/arraylist\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Java ArrayList Example &#8211; How to use ArrayList (with video)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/5fc06d4d50814931c15af68106832a56\",\"name\":\"Konstantina Dimtsa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Konstantina-Dimtsa-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Konstantina-Dimtsa-96x96.jpg\",\"caption\":\"Konstantina Dimtsa\"},\"description\":\"Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/konstantina-dimtsa\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java ArrayList - How to use (with video) - Examples Java Code Geeks","description":"The ArrayList class in Java supports only object types & not primitive types. Its methods are mostly used to add, remove elements in a list.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/","og_locale":"en_US","og_type":"article","og_title":"Java ArrayList - How to use (with video) - Examples Java Code Geeks","og_description":"The ArrayList class in Java supports only object types & not primitive types. Its methods are mostly used to add, remove elements in a list.","og_url":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-12-30T10:00:59+00:00","article_modified_time":"2022-07-05T20:28:54+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Konstantina Dimtsa","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Konstantina Dimtsa","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/"},"author":{"name":"Konstantina Dimtsa","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/5fc06d4d50814931c15af68106832a56"},"headline":"Java ArrayList Example &#8211; How to use ArrayList (with video)","datePublished":"2013-12-30T10:00:59+00:00","dateModified":"2022-07-05T20:28:54+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/"},"wordCount":881,"commentCount":1,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["featured"],"articleSection":["ArrayList"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/","url":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/","name":"Java ArrayList - How to use (with video) - Examples Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2013-12-30T10:00:59+00:00","dateModified":"2022-07-05T20:28:54+00:00","description":"The ArrayList class in Java supports only object types & not primitive types. Its methods are mostly used to add, remove elements in a list.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/arraylist-java-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"util","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/"},{"@type":"ListItem","position":5,"name":"ArrayList","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/arraylist\/"},{"@type":"ListItem","position":6,"name":"Java ArrayList Example &#8211; How to use ArrayList (with video)"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/5fc06d4d50814931c15af68106832a56","name":"Konstantina Dimtsa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Konstantina-Dimtsa-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/10\/Konstantina-Dimtsa-96x96.jpg","caption":"Konstantina Dimtsa"},"description":"Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.","sameAs":["http:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/konstantina-dimtsa\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/7811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=7811"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/7811\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=7811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=7811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=7811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}