java arrays reverse not understanding the logic -
i trying use code made reverse array. don't understand why getting [i@7a84639c
output in console.
and reason why doesn't method save reversed array array? if add print @ bottom of x[i]=c[i];
shows array reversed when add call example karn[0]
shows array isn't reversed. want solve staying true code made.
import java.util.arrays; public class helloworld { public static void main(string[] args) { int[]karn={1,2,3}; rev(karn); system.out.println(karn.tostring()); } public static void rev(int[]x){ int[]c=new int[x.length]; for(int i=x.length-1;i>-1;i--){ c[i]=x[i]; x[i]=c[i]; } } }
in rev
method using local variable c. value not transferred on main method. must return array , assign value old array:
public static int[] rev(int[]x){ //creates new array array different karn , local method. //nothing outside of method can see array. int[]c=new int[x.length]; for(int = 0; < c.length; i++){ //here swapping values placing first //value @ last spot of array , on c[c.length - - 1] = x[i]; } //we must return new array made , assign karn our //changes saved , seen outside of method return c; }
in main method must assign changes of rev
method karn. can assign value , display so:
karn = rev(karn); //for each loop for(int : karn) system.out.println(i); //traditional loop for(int = 0; < karn.length; i++) system.out.println(karn[i]);
arrays not have default tostring()
method. why seeing values of array like. need iterate through array display them console.
Comments
Post a Comment