1 /**2 * Enter characters to reverse the array output3 */4 ImportJava.util.Scanner;5 Public classinRESERVE6 {7 Public Static voidMain (string[] args)8 {9 reverse ();Ten } One Public Static voidreverse () { A -string[] arr =NewString[5]; - for(inti=0;i<arr.length;i++){ theScanner input =NewScanner (system.in); -System.out.println ("Please enter" + (i+1) + "characters"); -Arr[i] =Input.next (); - } + - for(intp=0,q=arr.length-1;p<q;p++,q--){ +String temp = "";//declaring temporary variables, storing arr[p] Atemp =Arr[p]; atARR[P] =Arr[q]; -ARR[Q] = temp;// - } -ToString (arr);//Call the ToString () method output array - } - in Public Static voidtoString (string[] arr) { -String str = "["; to for(inti=0;i<arr.length;i++){ +str + =Arr[i]; - if(i<arr.length-1){ thestr + = ","; * } $ }Panax Notoginsengstr + = "]"; - System.out.println (str); the } +}
Reverse output of Java Classic algorithm