java--to invert an array

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.