logo

Keer een array om in Java

In deze tutorial bespreken we hoe je dat kunt doen een array in Java omkeren . In de invoer wordt een integer-array gegeven en de taak is om de invoerarray om te keren. Het omkeren van een array betekent dat het laatste element van de invoerarray het eerste element van de omgekeerde array moet zijn, het op een na laatste element van de invoerarray het tweede element van de omgekeerde array moet zijn, enzovoort. Neem de volgende voorbeelden in acht.

Voorbeeld 1:

int naar string

Invoer:

arr[] = {1, 2, 3, 4, 5, 6, 7, 8}

Uitvoer

Voorbeeld 2:

Invoer:

arr[] = {4, 8, 3, 9, 0, 1}

Uitgang:

tekenreeks converteren naar int in Java

arr[] = {1, 0, 9, 3, 8, 4}

Benadering 1: gebruik van een hulparray

We kunnen de array van begin naar begin doorlopen, dat wil zeggen in omgekeerde volgorde, en het element opslaan dat door de lusindex wordt aangegeven in de hulparray. De hulparray bevat nu de elementen van de invoerarray in omgekeerde volgorde. Daarna kunnen we de hulparray op de console weergeven. Zie het volgende programma.

Bestandsnaam: ReverseArr.java

 public class ReverseArr { // method for reversing an array public int[] reverseArray(int arr[]) { // computing the size of the array arr int size = arr.length; // auxiliary array for reversing the // elements of the array arr int temp[] = new int[size]; int index = 0; for(int i = size - 1; i &gt;= 0; i--) { temp[i] = arr[index]; index = index + 1; } return temp; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr ReverseArr obj = new ReverseArr(); // input array - 1 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; int ans[] = obj.reverseArray(arr); System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + ' '); } system.out.println(); system.out.println('the reversed array is: for(int i="0;" < len; system.out.print(ans[i] system.out.println('
 input - int arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" ans1[]="obj.reverseArray(arr1);" system.out.println('for array: system.out.print(arr1[i] system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> A for loop is required to reverse the array, which makes the time complexity of the program O(n). Also, an auxiliary array is required to reverse the array making the space complexity of the program O(n), where n is the total number of elements present in the array.</p> <h2>Approach 2: Using Two Pointers</h2> <p>We can also use two pointers to reverse the input array. The first pointer will go to the first element of the array. The second pointer will point to the last element of the input array. Now we will start swapping elements pointed by these two pointers. After swapping, the second pointer will move in the leftward direction, and the first pointer will move in the rightward direction. When these two pointers meet or cross each other, we stop the swapping, and the array we get is the reversed array of the input array.</p> <p> <strong>FileName:</strong> ReverseArr1.java</p> <pre> public class ReverseArr1 { // method for reversing an array public int[] reverseArray(int arr[]) { // computing the size of the array arr int size = arr.length; // two pointers for reversing // the input array int ptr1 = 0; int ptr2 = size - 1; // reversing the input array // using a while loop while(ptr1 <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The time complexity of the program is the same as the previous program. There is no extra space used in the program, making the space complexity of the program O(1).</p> <h2>Approach 3: Using Stack</h2> <p>Since a Stack works on the LIFO (Last In First Out) principle, it can be used to reverse the input array. All we have to do is to put all the elements of the input array in the stack, starting from left to right. We will do it using a loop.</p> <p> <strong>FileName:</strong> ReverseArr2.java</p> <pre> // importing Stack import java.util.Stack; public class ReverseArr2 { // method for reversing an array public int[] reverseArray(int arr[]) { // computing the size of the array arr int size = arr.length; Stack stk = new Stack(); // pusing all the elements into stack // starting from left for(int i = 0; i <size; 1 2 i++) { stk.push(arr[i]); } int i="0;" while(!stk.isempty()) ele="stk.pop();" arr[i]="ele;" + 1; return arr; main method public static void main(string argvs[]) creating an object of the class reversearr2 obj="new" reversearr2(); input array - arr[]="{1," 2, 3, 4, 5, 6, 7, 8}; computing length len="arr.length;" system.out.println('for array: '); for(int < len; system.out.print(arr[i] ' ans[]="obj.reverseArray(arr);" system.out.println(); system.out.println('the reversed is: system.out.print(ans[i] system.out.println('
 arr1[]="{4," 8, 9, 0, 1}; system.out.print(arr1[i] ans1[]="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The time complexity of the program is the same as the previous program. There is stack used in the program, making the space complexity of the program O(n).</p> <h3>Using Recursion</h3> <p>Using recursion also, we can achieve the same result. Observe the following.</p> <p> <strong>FileName:</strong> ReverseArr3.java</p> <pre> // importing ArrayList import java.util.ArrayList; public class ReverseArr3 { ArrayList reverseArr; // constructor of the class ReverseArr3() { reverseArr = new ArrayList(); } // method for reversing an array public void reverseArray(int arr[], int i, int size) { // dealing with the base case if(i &gt;= size) { return; } // recursively calling the method reverseArray(arr, i + 1, size); reverseArr.add(arr[i]); } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr3 ReverseArr3 obj = new ReverseArr3(); // input array - 1 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 0 2 i++) { system.out.print(arr[i] + ' '); } obj.reversearray(arr, , len); system.out.println(); system.out.println('the reversed array is: for(int i="0;" < len; system.out.print(obj.reversearr.get(i) system.out.println('
 obj="new" reversearr3(); input - int arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println('for array: system.out.print(arr1[i] obj.reversearray(arr1, pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Explanation:</strong> The statement <em>reverseArr.add(arr[i]);</em> is written after the recursive call goes in the stack (note that the stack is implicit in this case). So, when the base case is hit in the recursive call, stack unwinding happens, and whatever is there in the stack pops out. The last element goes into the stack during the last recursive call. Therefore, the last element is popped out first. Then the penultimate element is popped out, and so on. The statement <em>reverseArr.add(arr[i]);</em> stores that popped element. In the end, we are displaying the elements that are stored in the list <em>reverseArr</em> .</p> <p> <strong>Complexity Analysis:</strong> Same as the first program of approach-3.</p> <h2>Approach 4: Using Collections.reverse() method</h2> <p>The build method Collections.reverse() can be used to reverse the list. The use of it is shown in the following program.</p> <p> <strong>FileName:</strong> ReverseArr4.java</p> <pre> // importing Collections, Arrays, ArrayList &amp; List import java.util.Collections; import java.util.Arrays; import java.util.List; import java.util.ArrayList; public class ReverseArr4 { // method for reversing an array public List reverseArray(Integer arr[]) { List l = (Arrays.asList(arr)); Collections.reverse(l); return l; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr4 ReverseArr4 obj = new ReverseArr4(); // input array - 1 Integer arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + ' '); } list ans="obj.reverseArray(arr);" system.out.println(); system.out.println('the reversed array is: for(int i="0;" < len; system.out.print(ans.get(i) system.out.println('
 input - integer arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println('for array: system.out.print(arr1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The program uses <em>Collections.reverse()</em> method that reverses the list in linear time, making the time complexity of the program O(n). The program uses using list, making the space complexity of the program O(n), where n is the total number of elements present in the array.</p> <h4>Note 1: <em>Collections.reverse()</em> method is also used to reverse the linked list.</h4> <h4>Note 2: All the approaches discussed above are applicable to different data types too.</h4> <h2>Approach 5: Using StringBuilder.append() method</h2> <p>It is evident from the heading that this approach is applicable to string arrays. Using the StringBuilder.append() method, we can reverse the string array. All we have to do is to start appending the string elements of the array from the last to the beginning.</p> <p> <strong>FileName:</strong> ReverseArr5.java</p> <pre> import java.util.*; public class ReverseArr5 { // method for reversing an array public String[] reverseArray(String arr[]) { StringBuilder reversedSB = new StringBuilder(); for (int j = arr.length; j &gt; 0; j--) { reversedSB.append(arr[j - 1]).append(&apos; &apos;); }; String[] reversedArr = reversedSB.toString().split(&apos; &apos;); return reversedArr; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr5 ReverseArr5 obj = new ReverseArr5(); // input array - 1 String arr[] = {&apos;javaTpoint&apos;, &apos;is&apos;, &apos;the&apos;, &apos;best&apos;, &apos;website&apos;}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + ' '); } string[] ans="obj.reverseArray(arr);" system.out.println(); system.out.println('the reversed array is: for(int i="0;" < len; system.out.print(ans[i] system.out.println('
 input - string arr1[]="{&apos;India&apos;," 'is', 'my', 'country'}; computing the length len="arr1.length;" system.out.println('for array: system.out.print(arr1[i] ans1="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: javaTpoint is the best website The reversed array is: website best the is javaTpoint For the input array: India is my country The reversed array is: country my is India </pre> <p> <strong>Complexity Analysis:</strong> The time and space complexity of the program is the same as the previous program.</p> <hr></len;></pre></len;></pre></len;></pre></size;></pre></pre></len;>

Complexiteitsanalyse: Er is een for-lus nodig om de array om te keren, wat de tijdcomplexiteit van het programma O(n) maakt. Ook is er een hulparray nodig om de array om te keren, waardoor de ruimtecomplexiteit van het programma O(n) wordt gemaakt, waarbij n het totale aantal elementen is dat in de array aanwezig is.

Benadering 2: gebruik van twee aanwijzers

We kunnen ook twee pointers gebruiken om de invoerarray om te keren. De eerste pointer gaat naar het eerste element van de array. De tweede pointer wijst naar het laatste element van de invoerarray. Nu zullen we beginnen met het verwisselen van elementen die door deze twee aanwijzingen worden aangegeven. Na het wisselen beweegt de tweede wijzer naar links en de eerste wijzer naar rechts. Wanneer deze twee pointers elkaar ontmoeten of kruisen, stoppen we met wisselen, en de array die we krijgen is de omgekeerde array van de invoerarray.

Bestandsnaam: OmgekeerdArr1.java

 public class ReverseArr1 { // method for reversing an array public int[] reverseArray(int arr[]) { // computing the size of the array arr int size = arr.length; // two pointers for reversing // the input array int ptr1 = 0; int ptr2 = size - 1; // reversing the input array // using a while loop while(ptr1 <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The time complexity of the program is the same as the previous program. There is no extra space used in the program, making the space complexity of the program O(1).</p> <h2>Approach 3: Using Stack</h2> <p>Since a Stack works on the LIFO (Last In First Out) principle, it can be used to reverse the input array. All we have to do is to put all the elements of the input array in the stack, starting from left to right. We will do it using a loop.</p> <p> <strong>FileName:</strong> ReverseArr2.java</p> <pre> // importing Stack import java.util.Stack; public class ReverseArr2 { // method for reversing an array public int[] reverseArray(int arr[]) { // computing the size of the array arr int size = arr.length; Stack stk = new Stack(); // pusing all the elements into stack // starting from left for(int i = 0; i <size; 1 2 i++) { stk.push(arr[i]); } int i="0;" while(!stk.isempty()) ele="stk.pop();" arr[i]="ele;" + 1; return arr; main method public static void main(string argvs[]) creating an object of the class reversearr2 obj="new" reversearr2(); input array - arr[]="{1," 2, 3, 4, 5, 6, 7, 8}; computing length len="arr.length;" system.out.println(\'for array: \'); for(int < len; system.out.print(arr[i] \' ans[]="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed is: system.out.print(ans[i] system.out.println(\'
 arr1[]="{4," 8, 9, 0, 1}; system.out.print(arr1[i] ans1[]="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The time complexity of the program is the same as the previous program. There is stack used in the program, making the space complexity of the program O(n).</p> <h3>Using Recursion</h3> <p>Using recursion also, we can achieve the same result. Observe the following.</p> <p> <strong>FileName:</strong> ReverseArr3.java</p> <pre> // importing ArrayList import java.util.ArrayList; public class ReverseArr3 { ArrayList reverseArr; // constructor of the class ReverseArr3() { reverseArr = new ArrayList(); } // method for reversing an array public void reverseArray(int arr[], int i, int size) { // dealing with the base case if(i &gt;= size) { return; } // recursively calling the method reverseArray(arr, i + 1, size); reverseArr.add(arr[i]); } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr3 ReverseArr3 obj = new ReverseArr3(); // input array - 1 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 0 2 i++) { system.out.print(arr[i] + \' \'); } obj.reversearray(arr, , len); system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(obj.reversearr.get(i) system.out.println(\'
 obj="new" reversearr3(); input - int arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] obj.reversearray(arr1, pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Explanation:</strong> The statement <em>reverseArr.add(arr[i]);</em> is written after the recursive call goes in the stack (note that the stack is implicit in this case). So, when the base case is hit in the recursive call, stack unwinding happens, and whatever is there in the stack pops out. The last element goes into the stack during the last recursive call. Therefore, the last element is popped out first. Then the penultimate element is popped out, and so on. The statement <em>reverseArr.add(arr[i]);</em> stores that popped element. In the end, we are displaying the elements that are stored in the list <em>reverseArr</em> .</p> <p> <strong>Complexity Analysis:</strong> Same as the first program of approach-3.</p> <h2>Approach 4: Using Collections.reverse() method</h2> <p>The build method Collections.reverse() can be used to reverse the list. The use of it is shown in the following program.</p> <p> <strong>FileName:</strong> ReverseArr4.java</p> <pre> // importing Collections, Arrays, ArrayList &amp; List import java.util.Collections; import java.util.Arrays; import java.util.List; import java.util.ArrayList; public class ReverseArr4 { // method for reversing an array public List reverseArray(Integer arr[]) { List l = (Arrays.asList(arr)); Collections.reverse(l); return l; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr4 ReverseArr4 obj = new ReverseArr4(); // input array - 1 Integer arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } list ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans.get(i) system.out.println(\'
 input - integer arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The program uses <em>Collections.reverse()</em> method that reverses the list in linear time, making the time complexity of the program O(n). The program uses using list, making the space complexity of the program O(n), where n is the total number of elements present in the array.</p> <h4>Note 1: <em>Collections.reverse()</em> method is also used to reverse the linked list.</h4> <h4>Note 2: All the approaches discussed above are applicable to different data types too.</h4> <h2>Approach 5: Using StringBuilder.append() method</h2> <p>It is evident from the heading that this approach is applicable to string arrays. Using the StringBuilder.append() method, we can reverse the string array. All we have to do is to start appending the string elements of the array from the last to the beginning.</p> <p> <strong>FileName:</strong> ReverseArr5.java</p> <pre> import java.util.*; public class ReverseArr5 { // method for reversing an array public String[] reverseArray(String arr[]) { StringBuilder reversedSB = new StringBuilder(); for (int j = arr.length; j &gt; 0; j--) { reversedSB.append(arr[j - 1]).append(&apos; &apos;); }; String[] reversedArr = reversedSB.toString().split(&apos; &apos;); return reversedArr; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr5 ReverseArr5 obj = new ReverseArr5(); // input array - 1 String arr[] = {&apos;javaTpoint&apos;, &apos;is&apos;, &apos;the&apos;, &apos;best&apos;, &apos;website&apos;}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } string[] ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans[i] system.out.println(\'
 input - string arr1[]="{&apos;India&apos;," \'is\', \'my\', \'country\'}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] ans1="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: javaTpoint is the best website The reversed array is: website best the is javaTpoint For the input array: India is my country The reversed array is: country my is India </pre> <p> <strong>Complexity Analysis:</strong> The time and space complexity of the program is the same as the previous program.</p> <hr></len;></pre></len;></pre></len;></pre></size;></pre>

Complexiteitsanalyse: De tijdscomplexiteit van het programma is hetzelfde als die van het vorige programma. Er wordt geen extra ruimte gebruikt in het programma, waardoor de ruimtecomplexiteit van het programma O(1) wordt.

Benadering 3: Stack gebruiken

Omdat een Stack werkt volgens het LIFO-principe (Last In First Out), kan deze worden gebruikt om de invoerarray om te keren. Het enige wat we hoeven te doen is alle elementen van de invoerarray in de stapel te plaatsen, beginnend van links naar rechts. We zullen het doen met behulp van een lus.

Bestandsnaam: OmgekeerdArr2.java

Java while-lus
 // importing Stack import java.util.Stack; public class ReverseArr2 { // method for reversing an array public int[] reverseArray(int arr[]) { // computing the size of the array arr int size = arr.length; Stack stk = new Stack(); // pusing all the elements into stack // starting from left for(int i = 0; i <size; 1 2 i++) { stk.push(arr[i]); } int i="0;" while(!stk.isempty()) ele="stk.pop();" arr[i]="ele;" + 1; return arr; main method public static void main(string argvs[]) creating an object of the class reversearr2 obj="new" reversearr2(); input array - arr[]="{1," 2, 3, 4, 5, 6, 7, 8}; computing length len="arr.length;" system.out.println(\'for array: \'); for(int < len; system.out.print(arr[i] \' ans[]="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed is: system.out.print(ans[i] system.out.println(\'
 arr1[]="{4," 8, 9, 0, 1}; system.out.print(arr1[i] ans1[]="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The time complexity of the program is the same as the previous program. There is stack used in the program, making the space complexity of the program O(n).</p> <h3>Using Recursion</h3> <p>Using recursion also, we can achieve the same result. Observe the following.</p> <p> <strong>FileName:</strong> ReverseArr3.java</p> <pre> // importing ArrayList import java.util.ArrayList; public class ReverseArr3 { ArrayList reverseArr; // constructor of the class ReverseArr3() { reverseArr = new ArrayList(); } // method for reversing an array public void reverseArray(int arr[], int i, int size) { // dealing with the base case if(i &gt;= size) { return; } // recursively calling the method reverseArray(arr, i + 1, size); reverseArr.add(arr[i]); } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr3 ReverseArr3 obj = new ReverseArr3(); // input array - 1 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 0 2 i++) { system.out.print(arr[i] + \' \'); } obj.reversearray(arr, , len); system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(obj.reversearr.get(i) system.out.println(\'
 obj="new" reversearr3(); input - int arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] obj.reversearray(arr1, pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Explanation:</strong> The statement <em>reverseArr.add(arr[i]);</em> is written after the recursive call goes in the stack (note that the stack is implicit in this case). So, when the base case is hit in the recursive call, stack unwinding happens, and whatever is there in the stack pops out. The last element goes into the stack during the last recursive call. Therefore, the last element is popped out first. Then the penultimate element is popped out, and so on. The statement <em>reverseArr.add(arr[i]);</em> stores that popped element. In the end, we are displaying the elements that are stored in the list <em>reverseArr</em> .</p> <p> <strong>Complexity Analysis:</strong> Same as the first program of approach-3.</p> <h2>Approach 4: Using Collections.reverse() method</h2> <p>The build method Collections.reverse() can be used to reverse the list. The use of it is shown in the following program.</p> <p> <strong>FileName:</strong> ReverseArr4.java</p> <pre> // importing Collections, Arrays, ArrayList &amp; List import java.util.Collections; import java.util.Arrays; import java.util.List; import java.util.ArrayList; public class ReverseArr4 { // method for reversing an array public List reverseArray(Integer arr[]) { List l = (Arrays.asList(arr)); Collections.reverse(l); return l; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr4 ReverseArr4 obj = new ReverseArr4(); // input array - 1 Integer arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } list ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans.get(i) system.out.println(\'
 input - integer arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The program uses <em>Collections.reverse()</em> method that reverses the list in linear time, making the time complexity of the program O(n). The program uses using list, making the space complexity of the program O(n), where n is the total number of elements present in the array.</p> <h4>Note 1: <em>Collections.reverse()</em> method is also used to reverse the linked list.</h4> <h4>Note 2: All the approaches discussed above are applicable to different data types too.</h4> <h2>Approach 5: Using StringBuilder.append() method</h2> <p>It is evident from the heading that this approach is applicable to string arrays. Using the StringBuilder.append() method, we can reverse the string array. All we have to do is to start appending the string elements of the array from the last to the beginning.</p> <p> <strong>FileName:</strong> ReverseArr5.java</p> <pre> import java.util.*; public class ReverseArr5 { // method for reversing an array public String[] reverseArray(String arr[]) { StringBuilder reversedSB = new StringBuilder(); for (int j = arr.length; j &gt; 0; j--) { reversedSB.append(arr[j - 1]).append(&apos; &apos;); }; String[] reversedArr = reversedSB.toString().split(&apos; &apos;); return reversedArr; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr5 ReverseArr5 obj = new ReverseArr5(); // input array - 1 String arr[] = {&apos;javaTpoint&apos;, &apos;is&apos;, &apos;the&apos;, &apos;best&apos;, &apos;website&apos;}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } string[] ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans[i] system.out.println(\'
 input - string arr1[]="{&apos;India&apos;," \'is\', \'my\', \'country\'}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] ans1="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: javaTpoint is the best website The reversed array is: website best the is javaTpoint For the input array: India is my country The reversed array is: country my is India </pre> <p> <strong>Complexity Analysis:</strong> The time and space complexity of the program is the same as the previous program.</p> <hr></len;></pre></len;></pre></len;></pre></size;>

Complexiteitsanalyse: De tijdscomplexiteit van het programma is hetzelfde als die van het vorige programma. Er wordt stack gebruikt in het programma, waardoor de ruimtecomplexiteit van het programma O(n) wordt.

Recursie gebruiken

Door ook recursie te gebruiken, kunnen we hetzelfde resultaat bereiken. Let op het volgende.

Bestandsnaam: OmgekeerdArr3.java

 // importing ArrayList import java.util.ArrayList; public class ReverseArr3 { ArrayList reverseArr; // constructor of the class ReverseArr3() { reverseArr = new ArrayList(); } // method for reversing an array public void reverseArray(int arr[], int i, int size) { // dealing with the base case if(i &gt;= size) { return; } // recursively calling the method reverseArray(arr, i + 1, size); reverseArr.add(arr[i]); } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr3 ReverseArr3 obj = new ReverseArr3(); // input array - 1 int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 0 2 i++) { system.out.print(arr[i] + \' \'); } obj.reversearray(arr, , len); system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(obj.reversearr.get(i) system.out.println(\'
 obj="new" reversearr3(); input - int arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] obj.reversearray(arr1, pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Explanation:</strong> The statement <em>reverseArr.add(arr[i]);</em> is written after the recursive call goes in the stack (note that the stack is implicit in this case). So, when the base case is hit in the recursive call, stack unwinding happens, and whatever is there in the stack pops out. The last element goes into the stack during the last recursive call. Therefore, the last element is popped out first. Then the penultimate element is popped out, and so on. The statement <em>reverseArr.add(arr[i]);</em> stores that popped element. In the end, we are displaying the elements that are stored in the list <em>reverseArr</em> .</p> <p> <strong>Complexity Analysis:</strong> Same as the first program of approach-3.</p> <h2>Approach 4: Using Collections.reverse() method</h2> <p>The build method Collections.reverse() can be used to reverse the list. The use of it is shown in the following program.</p> <p> <strong>FileName:</strong> ReverseArr4.java</p> <pre> // importing Collections, Arrays, ArrayList &amp; List import java.util.Collections; import java.util.Arrays; import java.util.List; import java.util.ArrayList; public class ReverseArr4 { // method for reversing an array public List reverseArray(Integer arr[]) { List l = (Arrays.asList(arr)); Collections.reverse(l); return l; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr4 ReverseArr4 obj = new ReverseArr4(); // input array - 1 Integer arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } list ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans.get(i) system.out.println(\'
 input - integer arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The program uses <em>Collections.reverse()</em> method that reverses the list in linear time, making the time complexity of the program O(n). The program uses using list, making the space complexity of the program O(n), where n is the total number of elements present in the array.</p> <h4>Note 1: <em>Collections.reverse()</em> method is also used to reverse the linked list.</h4> <h4>Note 2: All the approaches discussed above are applicable to different data types too.</h4> <h2>Approach 5: Using StringBuilder.append() method</h2> <p>It is evident from the heading that this approach is applicable to string arrays. Using the StringBuilder.append() method, we can reverse the string array. All we have to do is to start appending the string elements of the array from the last to the beginning.</p> <p> <strong>FileName:</strong> ReverseArr5.java</p> <pre> import java.util.*; public class ReverseArr5 { // method for reversing an array public String[] reverseArray(String arr[]) { StringBuilder reversedSB = new StringBuilder(); for (int j = arr.length; j &gt; 0; j--) { reversedSB.append(arr[j - 1]).append(&apos; &apos;); }; String[] reversedArr = reversedSB.toString().split(&apos; &apos;); return reversedArr; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr5 ReverseArr5 obj = new ReverseArr5(); // input array - 1 String arr[] = {&apos;javaTpoint&apos;, &apos;is&apos;, &apos;the&apos;, &apos;best&apos;, &apos;website&apos;}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } string[] ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans[i] system.out.println(\'
 input - string arr1[]="{&apos;India&apos;," \'is\', \'my\', \'country\'}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] ans1="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: javaTpoint is the best website The reversed array is: website best the is javaTpoint For the input array: India is my country The reversed array is: country my is India </pre> <p> <strong>Complexity Analysis:</strong> The time and space complexity of the program is the same as the previous program.</p> <hr></len;></pre></len;></pre></len;>

Uitleg: De verklaring omgekeerdeArr.add(arr[i]); wordt geschreven nadat de recursieve aanroep in de stapel is geplaatst (merk op dat de stapel in dit geval impliciet is). Dus wanneer het basisscenario wordt geraakt in de recursieve call, vindt het afwikkelen van de stapel plaats en komt alles wat zich in de stapel bevindt eruit. Het laatste element wordt tijdens de laatste recursieve aanroep in de stapel geplaatst. Daarom wordt het laatste element als eerste eruit gehaald. Vervolgens wordt het voorlaatste element eruit gehaald, enzovoort. De verklaring omgekeerdeArr.add(arr[i]); slaat dat gesprongen element op. Uiteindelijk geven we de elementen weer die in de lijst zijn opgeslagen achteruitArr .

Complexiteitsanalyse: Hetzelfde als het eerste programma van aanpak-3.

Benadering 4: De Collections.reverse()-methode gebruiken

De bouwmethode Collections.reverse() kan worden gebruikt om de lijst om te keren. Het gebruik ervan wordt getoond in het volgende programma.

Bestandsnaam: OmgekeerdArr4.java

 // importing Collections, Arrays, ArrayList &amp; List import java.util.Collections; import java.util.Arrays; import java.util.List; import java.util.ArrayList; public class ReverseArr4 { // method for reversing an array public List reverseArray(Integer arr[]) { List l = (Arrays.asList(arr)); Collections.reverse(l); return l; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr4 ReverseArr4 obj = new ReverseArr4(); // input array - 1 Integer arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } list ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans.get(i) system.out.println(\'
 input - integer arr1[]="{4," 8, 3, 9, 0, 1}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: 1 2 3 4 5 6 7 8 The reversed array is: 8 7 6 5 4 3 2 1 For the input array: 4 8 3 9 0 1 The reversed array is: 1 0 9 3 8 4 </pre> <p> <strong>Complexity Analysis:</strong> The program uses <em>Collections.reverse()</em> method that reverses the list in linear time, making the time complexity of the program O(n). The program uses using list, making the space complexity of the program O(n), where n is the total number of elements present in the array.</p> <h4>Note 1: <em>Collections.reverse()</em> method is also used to reverse the linked list.</h4> <h4>Note 2: All the approaches discussed above are applicable to different data types too.</h4> <h2>Approach 5: Using StringBuilder.append() method</h2> <p>It is evident from the heading that this approach is applicable to string arrays. Using the StringBuilder.append() method, we can reverse the string array. All we have to do is to start appending the string elements of the array from the last to the beginning.</p> <p> <strong>FileName:</strong> ReverseArr5.java</p> <pre> import java.util.*; public class ReverseArr5 { // method for reversing an array public String[] reverseArray(String arr[]) { StringBuilder reversedSB = new StringBuilder(); for (int j = arr.length; j &gt; 0; j--) { reversedSB.append(arr[j - 1]).append(&apos; &apos;); }; String[] reversedArr = reversedSB.toString().split(&apos; &apos;); return reversedArr; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr5 ReverseArr5 obj = new ReverseArr5(); // input array - 1 String arr[] = {&apos;javaTpoint&apos;, &apos;is&apos;, &apos;the&apos;, &apos;best&apos;, &apos;website&apos;}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \' \'); } string[] ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\'the reversed array is: for(int i="0;" < len; system.out.print(ans[i] system.out.println(\'
 input - string arr1[]="{&apos;India&apos;," \'is\', \'my\', \'country\'}; computing the length len="arr1.length;" system.out.println(\'for array: system.out.print(arr1[i] ans1="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: javaTpoint is the best website The reversed array is: website best the is javaTpoint For the input array: India is my country The reversed array is: country my is India </pre> <p> <strong>Complexity Analysis:</strong> The time and space complexity of the program is the same as the previous program.</p> <hr></len;></pre></len;>

Complexiteitsanalyse: Het programma maakt gebruik van Collecties.reverse() methode die de lijst in lineaire tijd omkeert, waardoor de tijdcomplexiteit van het programma O(n) wordt gemaakt. Het programma gebruikt list, waardoor de ruimtecomplexiteit van het programma O(n) wordt, waarbij n het totale aantal elementen in de array is.

staan

Notitie 1: Collecties.reverse() methode wordt ook gebruikt om de gekoppelde lijst om te keren.

Opmerking 2: Alle hierboven besproken benaderingen zijn ook van toepassing op verschillende gegevenstypen.

Benadering 5: De methode StringBuilder.append() gebruiken

Uit de kop blijkt duidelijk dat deze benadering van toepassing is op stringarrays. Met behulp van de methode StringBuilder.append() kunnen we de stringarray omkeren. Het enige wat we hoeven te doen is beginnen met het toevoegen van de stringelementen van de array, van de laatste tot de eerste plaats.

Bestandsnaam: OmgekeerdArr5.java

 import java.util.*; public class ReverseArr5 { // method for reversing an array public String[] reverseArray(String arr[]) { StringBuilder reversedSB = new StringBuilder(); for (int j = arr.length; j &gt; 0; j--) { reversedSB.append(arr[j - 1]).append(&apos; &apos;); }; String[] reversedArr = reversedSB.toString().split(&apos; &apos;); return reversedArr; } // main method public static void main(String argvs[]) { // creating an object of the class ReverseArr5 ReverseArr5 obj = new ReverseArr5(); // input array - 1 String arr[] = {&apos;javaTpoint&apos;, &apos;is&apos;, &apos;the&apos;, &apos;best&apos;, &apos;website&apos;}; // computing the length int len = arr.length; System.out.println(&apos;For the input array: &apos;); for(int i = 0; i <len; 2 i++) { system.out.print(arr[i] + \\' \\'); } string[] ans="obj.reverseArray(arr);" system.out.println(); system.out.println(\\'the reversed array is: for(int i="0;" < len; system.out.print(ans[i] system.out.println(\\'
 input - string arr1[]="{&apos;India&apos;," \\'is\\', \\'my\\', \\'country\\'}; computing the length len="arr1.length;" system.out.println(\\'for array: system.out.print(arr1[i] ans1="obj.reverseArray(arr1);" system.out.print(ans1[i] pre> <p> <strong>Output:</strong> </p> <pre> For the input array: javaTpoint is the best website The reversed array is: website best the is javaTpoint For the input array: India is my country The reversed array is: country my is India </pre> <p> <strong>Complexity Analysis:</strong> The time and space complexity of the program is the same as the previous program.</p> <hr></len;>

Complexiteitsanalyse: De tijd- en ruimtecomplexiteit van het programma is hetzelfde als die van het vorige programma.