Quantcast
Channel: How do I reverse an int array in Java? - Stack Overflow
Browsing all 49 articles
Browse latest View live

Answer by Percy Vega for How do I reverse an int array in Java?

For Java 8+, you can use the following to reverse an int[]:int[] validData = {1, 2, 3};validData = Arrays.stream(validData) .boxed() .sorted(Collections.reverseOrder()) .mapToInt(Integer::intValue)...

View Article



Answer by Maninder for How do I reverse an int array in Java?

Simple way to do this: for(int i=queue.length-1;i>=0;i--){ System.out.print(queue[i] +" "); }

View Article

Answer by krishna T for How do I reverse an int array in Java?

A implementation using generics for arrays of non primitive types. //Reverse and get new Array -preferred public static final <T> T[] reverse(final T[] array) { final int len = array.length;...

View Article

Answer by Aalishan Ansari for How do I reverse an int array in Java?

This has 2 solutionLoopRecursionpublic class _1_ReverseArray { public static void main(String[] args) { int array[] = {2, 3, 1, 4, 9}; //reverseArray(array, 0, array.length - 1);...

View Article

Answer by Martin Kersten for How do I reverse an int array in Java?

Just for the sake of it. People often do only need a 'view' on an array or list in reversed order instead of a completely do not need a reversed array when working with streams and collections but a...

View Article


Answer by James Guest for How do I reverse an int array in Java?

Here is a condensed version:My solution creates a new array reversedWith each iteration of i the for loop inserts the last index [array.length - 1] into the current index [i]Then continues the same...

View Article

Answer by Bwizz for How do I reverse an int array in Java?

A short way to reverse without additional libraries, imports, or static references.int[] a = {1,2,3,4,5,6,7,23,9}, b; //compound declarationvar j = a.length;b = new int[j];for (var i : a) b[--j] = i;...

View Article

Image may be NSFW.
Clik here to view.

Answer by Nasib for How do I reverse an int array in Java?

a piece of cake.i did it for string but, it's not much different

View Article


Answer by Ahmad Dalao for How do I reverse an int array in Java?

There are some great answers above, but this is how I did it:public static int[] test(int[] arr) { int[] output = arr.clone(); for (int i = arr.length - 1; i > -1; i--) { output[i] = arr[arr.length...

View Article


Answer by Kalidindi Prashanth for How do I reverse an int array in Java?

public static void main(String args[]) { int [] arr = {10, 20, 30, 40, 50}; reverse(arr, arr.length); } private static void reverse(int[] arr, int length) { for(int i=length;i>0;i--) {...

View Article

Answer by roshan posakya for How do I reverse an int array in Java?

public static int[] reverse(int[] array) { int j = array.length-1; // swap the values at the left and right indices ////// for(int i=0; i<=j; i++) { int temp = array[i]; array[i] = array[j];...

View Article

Answer by Sameer Shrestha for How do I reverse an int array in Java?

2 ways to reverse an Array .Using For loop and swap the elements till the mid point with time complexity of O(n/2).private static void reverseArray() {int[] array = new int[] { 1, 2, 3, 4, 5, 6 };for...

View Article

Answer by Z A Abbasi for How do I reverse an int array in Java?

static int[] reverseArray(int[] a) { int ret[] = new int[a.length]; for(int i=0, j=a.length-1; i<a.length && j>=0; i++, j--) ret[i] = a[j]; return ret;}

View Article


Answer by Karan Khanna for How do I reverse an int array in Java?

There are two ways to have a solution for the problem:1. Reverse an array in space.Step 1. Swap the elements at the start and the end index.Step 2. Increment the start index decrement the end...

View Article

Answer by jagdish khetre for How do I reverse an int array in Java?

import java.util.Scanner;class ReverseArray { public static void main(String[] args) { int[] arra = new int[10]; Scanner sc = new Scanner(System.in); System.out.println("Enter Array Elements : ");...

View Article


Answer by user11016 for How do I reverse an int array in Java?

Solution with o(n) time complexity and o(1) space complexity.void reverse(int[] array) { int start = 0; int end = array.length - 1; while (start < end) { int temp = array[start]; array[start] =...

View Article

Answer by akhil_mittal for How do I reverse an int array in Java?

In case of Java 8 we can also use IntStream to reverse the array of integers as:int[] sample = new int[]{1,2,3,4,5};int size = sample.length;int[] reverseSample = IntStream.range(0,size).map(i ->...

View Article


Answer by Simple-Solution for How do I reverse an int array in Java?

Here is what I've come up with:// solution 1 - boiler plated Integer[] original = {100, 200, 300, 400};Integer[] reverse = new Integer[original.length];int lastIdx = original.length -1;int startIdx =...

View Article

Answer by Patrick Parker for How do I reverse an int array in Java?

Use a stream to reverseThere are already a lot of answers here, mostly focused on modifying the array in-place. But for the sake of completeness, here is another approach using Java streams to preserve...

View Article

Answer by Doncho Balamjiev for How do I reverse an int array in Java?

int[] arrTwo = {5, 8, 18, 6, 20, 50, 6}; for (int i = arrTwo.length-1; i > 0; i--) { System.out.print(arrTwo[i] +""); }

View Article
Browsing all 49 articles
Browse latest View live




Latest Images