{"id":1310,"date":"2016-09-22T07:18:54","date_gmt":"2016-09-22T07:18:54","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1310"},"modified":"2024-12-14T03:54:05","modified_gmt":"2024-12-14T03:54:05","slug":"array-reflection-in-the-mirror-java-program","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/array-reflection-in-the-mirror-java-program\/","title":{"rendered":"Array reflection in the mirror java program?"},"content":{"rendered":"<h2>Array reflection in the mirror java program?<\/h2>\n<h3><strong>Problem statement<\/strong><\/h3>\n<p>Array reflection in the mirror. Write a Java program to reverse an array i.e. from top to bottom, exchange the first row for the last row, the second row for the second to last row, and so on until the entire array is reversed.<\/p>\n<p>Note: I recommend the reader to try the program for practice, before reading down my solution.<\/p>\n<h2><strong>Java Program<\/strong><\/h2>\n<p>Assume array size 10\u00d710. Let&#8217;s write the Java program to display the array and its reflection in the mirror. In this program, the array will be filled with random numbers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package com.testingdocs.sample;\r\n\r\npublic class MirrorArrayExample {\r\n static int[][] array ;\r\n public static void main(String[] args) {\r\n \/\/ Array of size 10x10\r\n int n = 10;\r\n array = fillRandomArray(n);\r\n printArray(array);\r\n\r\nSystem.out.println(\"Mirror\" + \"----------------------------------------------------------------\");\r\n mirrorArray(array);\r\n printArray(array);\r\n\r\n}\r\n\r\n\/\/ fill the array with Random numbers from 0-9\r\n static int[][] fillRandomArray(int n) {\r\n int[][] a = new int[n][n];\r\n for(int i=0;i&lt;n;i++)\r\n {\r\n for(int j=0;j&lt;n;j++)\r\n {\r\n a[i][j] = getRandom(10);\r\n }\r\n }\r\n return a;\r\n }\r\n\r\n\/\/ print the array \r\n static int[][] printArray(int[][] a)\r\n {\r\n for(int i=0;i&lt; a.length;i++)\r\n {\r\n for(int j=0;j&lt;a.length;j++)\r\n {\r\n System.out.print(\" \" + a[i][j]);\r\n }\r\n System.out.println();\r\n }\r\n return a;\r\n }\r\n\r\nstatic int getRandom(int max){\r\n return (int) (Math.random()*max);\r\n }\r\n\r\nstatic void mirrorArray(int [][] a) {\r\n\r\nfor(int i = 0; i &lt; (a.length\/2); i++) {\r\n int[] temp = a[i];\r\n a[i] = a[a.length - i - 1];\r\n a[a.length - i - 1] = temp;\r\n }\r\n }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>Output of the program<\/strong><\/h3>\n<p>6 5 0 7 1 2 7 0 5 4<br \/>\n0 8 9 7 3 8 8 9 0 1<br \/>\n0 7 9 4 7 3 3 7 6 4<br \/>\n1 1 5 3 4 8 5 2 4 7<br \/>\n4 0 8 9 2 2 6 4 8 8<br \/>\n6 0 6 7 0 8 9 8 5 1<br \/>\n2 1 0 9 9 1 3 5 3 7<br \/>\n7 0 7 3 1 9 9 5 2 9<br \/>\n6 2 1 7 1 0 0 9 0 5<br \/>\n5 7 0 3 7 1 2 3 9 2<br \/>\nMirror\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014-<br \/>\n5 7 0 3 7 1 2 3 9 2<br \/>\n6 2 1 7 1 0 0 9 0 5<br \/>\n7 0 7 3 1 9 9 5 2 9<br \/>\n2 1 0 9 9 1 3 5 3 7<br \/>\n6 0 6 7 0 8 9 8 5 1<br \/>\n4 0 8 9 2 2 6 4 8 8<br \/>\n1 1 5 3 4 8 5 2 4 7<br \/>\n0 7 9 4 7 3 3 7 6 4<br \/>\n0 8 9 7 3 8 8 9 0 1<br \/>\n6 5 0 7 1 2 7 0 5 4<\/p>\n<h3><strong>Screenshot<\/strong><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1458\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-Reflection-in-Mirror.png\" alt=\"Array Reflection in Mirror\" width=\"1161\" height=\"478\" title=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array reflection in the mirror java program? Problem statement Array reflection in the mirror. Write a Java program to reverse an array i.e. from top to bottom, exchange the first row for the last row, the second row for the second to last row, and so on until the entire array is reversed. Note: I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1310","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1310"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1310\/revisions"}],"predecessor-version":[{"id":26361,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1310\/revisions\/26361"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}