Reverse a int array in java -


this question has answer here:

i've written following code reverse int array, however, not see result right. hint on this? thanks!

the result

[i@15db9742[i@15db9742[i@15db9742[i@15db9742[i@15db9742[i@15db9742[i@15db9742[i@ 

which seems weird.

public class reversearray { public static void main(string[] args) {     int[] array = {2, 4, 5, 7, 8, 9, 12, 14, 17, 19, 22, 25, 27, 28, 33, 37};     reverse(array, 0, array.length - 1); }  public static void reverse(int[] data, int low, int high) {      if (low < high) {         int temp = data[low];         data[low] = data[high];         data[high] = temp;         reverse(data, low + 1, high - 1);     }     system.out.print(data);  } } 

you system.out.print printing array object , not array index value.

use below:-

system.out.print(data[low]); 

you can print array:-

public static void main(string[] args) {         int[] array = {2, 4, 5, 7, 8, 9, 12, 14, 17, 19, 22, 25, 27, 28, 33, 37};         reverse(array, 0, array.length - 1);         printarray(array);     }      public static void reverse(int[] data, int low, int high) {          if (low < high) {             int temp = data[low];             data[low] = data[high];             data[high] = temp;             reverse(data, low + 1, high - 1);         }      }      private static void printarray(int [] array){         for(int i:array){             system.out.print(i+"|");         }         system.out.println();     }  output: 37|33|28|27|25|22|19|17|14|12|9|8|7|5|4|2| 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -