{"id":323,"date":"2021-01-03T22:05:05","date_gmt":"2021-01-03T22:05:05","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=323"},"modified":"2021-01-09T22:02:44","modified_gmt":"2021-01-09T22:02:44","slug":"multidimensional-arrays","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/objects-arrays\/multidimensional-arrays\/","title":{"rendered":"Multidimensional Arrays"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Multi-dimensional arrays in Java are simply&nbsp;<em>arrays of arrays.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Creating an array of arrays such as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>char lines[][] = new char[8][20];<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">produces the following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"497\" height=\"397\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/array2.gif\" alt=\"\" class=\"wp-image-326\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:31px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What do you think the following will print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>char lines[][] = new char[8][20];\nSystem.out.println(\n    \"char lines[][] has \" + lines.length + \" elements\");\nSystem.out.println(\n    \"lines[0] has \" + lines[0].length + \" elements\");\nSystem.out.println(\n    \"lines[0] contains: \" + lines[0]);<\/strong><\/pre>\n\n\n\n<div class=\"wp-block-group has-background\" style=\"background-color:#bbf5d2\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-pb-accordion-item c-accordion__item js-accordion-item no-js\" data-initially-open=\"false\" data-click-to-close=\"true\" data-auto-close=\"true\" data-scroll=\"false\" data-scroll-offset=\"0\"><h4 id=\"at-95325\" class=\"c-accordion__title js-accordion-controller\" role=\"button\">Click here for answer<\/h4><div id=\"ac-95325\" class=\"c-accordion__content\">\n<pre class=\"wp-block-preformatted\">char lines[][] has 8 elements\nlines[0] has 20 elements\nlines[0] contains:<\/pre>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What about the following?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>char line[][] = new char[8][<\/strong>\/* NOTHING *\/<strong>];\nSystem.out.println(\n    \"char line[][] has \" + line.length + \" elements\");\nSystem.out.println(\n    \"line[0] has \" + line[0].length + \" elements\");\nSystem.out.println(\n    \"line[0] contains: \" + line[0]);<\/strong><\/pre>\n\n\n\n<div class=\"wp-block-group has-background\" style=\"background-color:#bbf5d2\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-pb-accordion-item c-accordion__item js-accordion-item no-js\" data-initially-open=\"false\" data-click-to-close=\"true\" data-auto-close=\"true\" data-scroll=\"false\" data-scroll-offset=\"0\"><h4 id=\"at-55528\" class=\"c-accordion__title js-accordion-controller\" role=\"button\">Click here for answer<\/h4><div id=\"ac-55528\" class=\"c-accordion__content\">\n<pre id=\"block-ce374eb0-5ac4-40b2-890d-b63bf28ec3d7\" class=\"wp-block-preformatted\">ERROR: java.lang.NullPointerException<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<div style=\"height:35px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Note that, because Java multidimensional arrays are arrays of arrays, they need not be &#8216;rectangular&#8217;. For example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>short triangle[][] = new short[10][];\nfor (int i = 0; i &lt; triangle.length; i++)\n{\n    triangle[i] = new short[i+1];\n    for (int j = 0; j &lt; i+1; j++)\n        triangle[i][j] = (short)(i + j);\n}\nfor (int i = 0; i &lt; triangle.length; i++)\n{\n    for (int j = 0; j &lt; triangle[i].length; j++)\n    System.out.print(triangle[i][j] + \" \");\n    System.out.println();\n}\n<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">produces:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>0\n1 2\n2 3 4\n3 4 5 6\n4 5 6 7 8\n5 6 7 8 9 10\n6 7 8 9 10 11 12\n7 8 9 10 11 12 13 14\n8 9 10 11 12 13 14 15 16\n9 10 11 12 13 14 15 16 17 18<\/strong><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Multi-dimensional arrays in Java are simply&nbsp;arrays of arrays. Creating an array of arrays such as: char lines[][] = new char[8][20]; produces the following: What do you think the following will print? char lines[][] = new char[8][20]; System.out.println( &#8220;char lines[][] has &#8221; + lines.length + &#8221; elements&#8221;); System.out.println( &#8220;lines[0] has &#8221; + lines[0].length + &#8221; elements&#8221;); [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":318,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","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":"0","ocean_custom_header_template":"0","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":"0","ocean_menu_typo_font_family":"0","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":"0","footnotes":""},"class_list":["post-323","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Multi-dimensional arrays in Java are simply&nbsp;arrays of arrays. Creating an array of arrays such as: char lines[][] = new char[8][20]; produces the following: What do you think the following will print? char lines[][] = new char[8][20]; System.out.println( \"char lines[][] has \" + lines.length + \" elements\"); System.out.println( \"lines[0] has \" + lines[0].length + \" elements\");&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":9,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/323\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/323\/revisions\/610"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/318"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}