{"id":1154,"date":"2023-11-12T00:00:00","date_gmt":"2023-11-12T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1154"},"modified":"2024-02-06T11:50:16","modified_gmt":"2024-02-06T16:50:16","slug":"java-converting-string-to-list","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/java-converting-string-to-list\/","title":{"rendered":"Java: Converting String to List"},"content":{"rendered":"\n<p>There are many other reasons for wanting to convert a String to a List. For example, the length of the String needs to be defined. Such a restriction does not exist in an ArrayList. \u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import java.util.*;\r\n\r\npublic class StringToList {\r\n   public static void main(String[] args) {\r\n       \/\/ initialize string array\r\n       String[] w = {\"convert\", \"string\", \"to\", \"list\"};\r\n       \/\/ convert to string array to list\r\n       List&lt;String> myList = Arrays.asList(w);  \r\n       \/\/ add to ArrayList\r\n       ArrayList al = new ArrayList(myList);\r\n       \/\/ print the list contents\r\n       System.out.println(al.toString());\r\n   }  \r\n}<\/pre>\n\n\n\n<p>Arrays.asList() is the function which actually converts String to List. The remainder of the code is show a working example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are many other reasons for wanting to convert a String to a List. For example, the length of the String needs to be defined. Such a restriction does not exist in an ArrayList. \u00a0 import java.util.*; public class StringToList { public static void main(String[] args) { \/\/ initialize string array String[] w = {&#8220;convert&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1212,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38,76,65],"class_list":["post-1154","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-programming","tag-string-manipulation"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1154","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=1154"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1154\/revisions"}],"predecessor-version":[{"id":1155,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1154\/revisions\/1155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1212"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}