Quantcast
Channel: How do I reverse an int array in Java? - Stack Overflow
Browsing index pages (50 articles)
↧

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

Two simple options could be:Few options use Apache library Commons.Lang, ArrayUtils.reverse(int[] array)Use Java stream api (Java_Version >= 1.8):int[] reverseSortedArray =...

View Article


How do I reverse an int array in Java?

I am trying to reverse an int array in Java.This method does not reverse the array. for(int i = 0; i < validData.length; i++){ int temp = validData[i]; validData[i] = validData[validData.length - i...

View Article


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

public class ArrayHandle { public static Object[] reverse(Object[] arr) { List<Object> list = Arrays.asList(arr); Collections.reverse(list); return list.toArray(); }}

View Article

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

To reverse an int array, you swap items up until you reach the midpoint, like this:for(int i = 0; i < validData.length / 2; i++){ int temp = validData[i]; validData[i] = validData[validData.length -...

View Article

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

It is most efficient to simply iterate the array backwards.I'm not sure if Aaron's solution does this vi this call Collections.reverse(list); Does anyone know?

View Article


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

Your program will work for only length = 0, 1.You can try :int i = 0, j = validData.length-1 ; while(i < j){ swap(validData, i++, j--); // code for swap not shown, but easy enough}

View Article

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

With Commons.Lang, you could simply use ArrayUtils.reverse(int[] array)Most of the time, it's quicker and more bug-safe to stick with easily available libraries already unit-tested and user-tested when...

View Article

Answer by Bill the Lizard for How do I reverse an int array in Java?

I think it's a little bit easier to follow the logic of the algorithm if you declare explicit variables to keep track of the indices that you're swapping at each iteration of the loop.public static...

View Article


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

If working with data that is more primitive (i.e. char, byte, int, etc) then you can do some fun XOR operations.public static void reverseArray4(int[] array) { int len = array.length; for (int i = 0; i...

View Article


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

public void display(){ String x[]=new String [5]; for(int i = 4 ; i > = 0 ; i-- ){//runs backwards //i is the nums running backwards therefore its printing from //highest element to the lowest(ie...

View Article

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

public static void main (String args[]){ //create array String[] stuff ={"eggs","lasers","hats","pie","apples"}; //print out array for(String x :stuff) System.out.printf("%s ", x);...

View Article

Answer by Krishna Kumar Chourasiya for How do I reverse an int array in Java?

This will help youint a[] = {1,2,3,4,5};for (int k = 0; k < a.length/2; k++) { int temp = a[k]; a[k] = a[a.length-(1+k)]; a[a.length-(1+k)] = temp;}

View Article

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

This is how I would personally solve it. The reason behind creating the parametrized method is to allow any array to be sorted... not just your integers.I hope you glean something from it.@Testpublic...

View Article


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

Wouldn't doing it this way be much more unlikely for mistakes? int[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int[] temp = new int[intArray.length]; for(int i = intArray.length - 1; i > -1; i...

View Article

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

below is the complete program to run in your machine.public class ReverseArray { public static void main(String[] args) { int arr[] = new int[] { 10,20,30,50,70 }; System.out.println("reversing an...

View Article


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

private static int[] reverse(int[] array){ int[] reversedArray = new int[array.length]; for(int i = 0; i < array.length; i++){ reversedArray[i] = array[array.length - i - 1]; } return reversedArray;}

View Article

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

Using the XOR solution to avoid the temp variable your code should look likefor(int i = 0; i < validData.length; i++){ validData[i] = validData[i] ^ validData[validData.length - i - 1];...

View Article


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

public class TryReverse { public static void main(String[] args) { int [] array = {2,3,4,5,6,7,8,9}; reverse(array); for(int i=0; i<array.length; ++i) System.out.print(array[i] +""); } public static...

View Article

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

Simple for loop!for (int start = 0, end = array.length - 1; start <= end; start++, end--) { int aux = array[start]; array[start]=array[end]; array[end]=aux;}

View Article

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

Try this program in JAVA :-import java.util.Scanner;public class Rev_one_D { static int row; static int[] trans_arr = new int[row]; public static void main(String[] args) { Scanner sc = new...

View Article
Browsing index pages (50 articles)


Latest Images