{"id":284,"date":"2021-01-25T12:00:00","date_gmt":"2021-01-25T17:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=284"},"modified":"2024-02-09T14:47:23","modified_gmt":"2024-02-09T19:47:23","slug":"java-linked-list","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/java-linked-list\/","title":{"rendered":"Java Linked List"},"content":{"rendered":"\n<p>The Internet is full of code examples on how you can create your own LinkedList class. Java provides and very easy to use class LinkedList. Following is an example on how to use a Java LinkedList.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.LinkedList;\n\npublic class LinkedListDemo {\n   public static void main(String&#91;] args) {   \n      \n      \/\/ initialize a LinkedList\n      LinkedList link = new LinkedList();&nbsp;&nbsp;&nbsp;\n      System.out.println(link); &nbsp;&nbsp;\/\/ empty linked list&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;\n\n      \/\/ add elements\n      link.add(\"linked\");\n      link.add(\"lists\");\n      System.out.println(link);\n\n      \/\/ add an element in the first position&nbsp;&nbsp;&nbsp;\n      link.addFirst(\"My\");\n      System.out.println(link);\n\n      \/\/ adding an element in a specific location\n      link.add(1, new Integer(10));\n      System.out.println(link);\n\n      link.remove(2);\n      System.out.println(link);\n\n      System.out.println(link.size());\n   }\n}<\/code><\/pre>\n\n\n\n<p><br><strong>output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;]\n&#91;linked, lists]\n&#91;My, linked, lists]\n&#91;My, 10, linked, lists]\n&#91;My, 10, lists]\n3<\/code><\/pre>\n\n\n\n<p>First, LinkedList is initialized. The print statement prints an empty list. Then two elements are added and printed. An element can also be added such that it is the first element using addFirst(). An element can be added at a specific location by specifying an index. Indices start from 0. remove() removes an element from the linked list. link.size() prints the size of the list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Internet is full of code examples on how you can create your own LinkedList class. Java provides and very easy to use class LinkedList. Following is an example on how to use a Java LinkedList. output First, LinkedList is initialized. The print statement prints an empty list. Then two elements are added and printed. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38,76],"class_list":["post-284","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-programming"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=284"}],"version-history":[{"count":2,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/284\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/284\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}