tag:blogger.com,1999:blog-694855878384792308.post2031196608670361192..comments2024-03-21T06:26:49.387-07:00Comments on Java67: [Solved] 3 Examples to reverse an Array in Java - Example Tutorialjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-694855878384792308.post-15623584240576169532018-06-22T22:50:37.178-07:002018-06-22T22:50:37.178-07:00int[] a = new int[]{1,5,6,8,9,10,12,15,16};
... int[] a = new int[]{1,5,6,8,9,10,12,15,16};<br /> int n=a.length;<br /> int[] b = new int[n];<br /> //reverse<br /> for(int i=0;i<n;i++){<br /> int temp=a[i];<br /> b[i]=a[n-i-1];<br /> a[i]=temp;<br /> System.out.println(b[i]);<br /> }<br /> <br />Anonymoushttps://www.blogger.com/profile/13303118881927727878noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-71891224643701375482016-10-03T05:40:24.712-07:002016-10-03T05:40:24.712-07:00No, you're not reversing anything. You are jus...No, you're not reversing anything. You are just printing it out reversed. That's not about what this article is saying. Also, your approach have a O(N*2) complexity and creating N arrays which is very inefficient.Anonymoushttps://www.blogger.com/profile/01898364035412236863noreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-89275357473151520402016-10-02T12:18:08.094-07:002016-10-02T12:18:08.094-07:00We can write following way also.
=================...We can write following way also.<br />=================================<br /><br />package com.main.java.resources;<br /><br />public class ArraysReverse {<br /> public static void main(String[] args) {<br /> String array[] = { "test1", "test2", "test3" };<br /> String arrayTemp[]=new String[array.length];<br /> for (int i = arrayTemp.length-1; i >= 0; i--) {<br /> for (int j = i; j <=i; j++) {<br /> arrayTemp[j]=array[i];<br /> System.out.println(arrayTemp[j]);<br /> }<br /> }<br /> }<br />}Anonymoushttps://www.blogger.com/profile/06179927658237960158noreply@blogger.com