The Java source code sample shows:
Public Static voidMain (string[] args) {String [] a= {"1", "2", "3"}; System.out.print ("Initial array A is: \ n"); for(String string:a) {System.out.print (string+ "\ T"); } String [] b=Reversearray (a); System.out.print ("\ nthe array b after the first reversal is: \ n"); for(String string:b) {System.out.print (string+ "\ T"); } System.out.print ("\ nthe array c after the second reversal is:"); String [] C=ReverseArray2 (b); System.out.println (); for(String string:c) {System.out.print (string+ "\ T"); } } /*** Inverse array *@paramArray *@return */ Public Staticstring[] Reversearray (string[] array) {String [] newarray=NewString[array.length]; for(inti=0; i<newarray.length; i++) {Newarray[i]= Array[array.length-i-1]; } returnNewArray; } Public Staticstring[] ReverseArray2 (string[] Array) {string[] New_array=NewString[array.length]; for(inti = 0; i < Array.Length; i++) { //the first element of the inverted array is equal to the last element of the source array:New_array[i] = array[array.length-i-1]; } returnNew_array; }
Results show:
This is just a simple example, interested can improve themselves, such as ① keyboard input array to execute, ② directly reverse the array of data, do not create a new array, ③ change to other data types, and so on, as long as you are happy, want to play how to play.
java--to invert an array