{"id":4686,"date":"2014-10-13T17:30:14","date_gmt":"2014-10-13T22:30:14","guid":{"rendered":"http:\/\/cis.bentley.edu\/sandbox\/?p=4686"},"modified":"2014-10-13T17:30:14","modified_gmt":"2014-10-13T22:30:14","slug":"java-a-complete-guide-to-array-and-arraylist","status":"publish","type":"post","link":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/2014\/10\/13\/java-a-complete-guide-to-array-and-arraylist\/","title":{"rendered":"Java: A Complete Guide to Array and ArrayList"},"content":{"rendered":"<p>Many students have confusions between Array and ArrayList in Java when it comes to the question of which to use. The major difference between this two is the <strong>static<\/strong> nature of Array and <strong>dynamic<\/strong> nature of ArrayList. Specifically, if you define and initialize an Array, you can never add or remove elements from it. However, you can always make changes to the size of an ArrayList.\u00a0Therefore, when you decide which to use, Array or ArrayList, the first thing you need to consider is whether you have a list of a <strong>fixed<\/strong> length or of a <strong>variable<\/strong> length.\u00a0Now Let&#8217;s move on to how to initialize and define Array and ArrayList.<\/p>\n<p>Initialize String array when you know all the elements:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4698\" alt=\"1\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/1.jpg\" width=\"297\" height=\"29\" \/><\/p>\n<p>If you want to make changes to one element of array (e.g. replace &#8220;red&#8221; with &#8220;orange&#8221;):<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/2.jpg\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4699\" alt=\"2\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/2.jpg\" width=\"150\" height=\"23\" \/><\/a><\/p>\n<p>Initialize String array when you only know the number of elements:<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/3.jpg\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4700\" alt=\"3\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/3.jpg\" width=\"234\" height=\"24\" \/><\/a><\/p>\n<p>Assign values to the array:<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/4.jpg\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4702\" alt=\"4\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/4.jpg\" width=\"157\" height=\"53\" \/><\/a><\/p>\n<p>You can get the length of the array by animal.length().<\/p>\n<p>Initialize ArrayList (Note: you cannot set initial size of an ArrayList):<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/Screenshot-72.png\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4708\" alt=\"Screenshot (72)\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/Screenshot-72.png\" width=\"372\" height=\"27\" \/><\/a><\/p>\n<p>Add elements to the ArrayList:<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/8.jpg\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4709\" alt=\"8\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/8.jpg\" width=\"171\" height=\"86\" \/><\/a><\/p>\n<p>Get size of the ArrayList:<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/9.jpg\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4710\" alt=\"9\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/9.jpg\" width=\"261\" height=\"26\" \/><\/a><\/p>\n<p>Remove the first element from the ArrayList:<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/Screenshot-75.png\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4711\" alt=\"Screenshot (75)\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/Screenshot-75.png\" width=\"130\" height=\"26\" \/><\/a><\/p>\n<p>Remove &#8220;Buffalo&#8221; from the ArrayList:<\/p>\n<p><a href=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/10.jpg\" rel=\"lightbox\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4712\" alt=\"10\" src=\"http:\/\/cis.bentley.edu\/sandbox\/wp-content\/uploads\/2014\/10\/10.jpg\" width=\"304\" height=\"84\" \/><\/a><\/p>\n<p>So when you decide whether to use Array or ArrayList, if you know the exact number of elements, use Array; if you don&#8217;t, use ArrayList.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many students have confusions between Array and ArrayList in Java when it comes to the question of which to use. The major difference between this two is the static nature of Array and dynamic nature of ArrayList. Specifically, if you define and initialize an Array, you can never add or remove elements from it. However, [&hellip;]<\/p>\n","protected":false},"author":152,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_mi_skip_tracking":false,"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[11],"tags":[],"class_list":["post-4686","post","type-post","status-publish","format-standard","hentry","category-java","entry"],"acf":[],"_links":{"self":[{"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/posts\/4686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/users\/152"}],"replies":[{"embeddable":true,"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/comments?post=4686"}],"version-history":[{"count":8,"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/posts\/4686\/revisions"}],"predecessor-version":[{"id":4714,"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/posts\/4686\/revisions\/4714"}],"wp:attachment":[{"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/media?parent=4686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/categories?post=4686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cissandbox.bentley.edu\/sandbox\/index.php\/wp-json\/wp\/v2\/tags?post=4686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}