Method of array flipping (Java implementation)

Source: Internet
Author: User

The method of array flipping (Java implementation), so-called array flipping, is to invert the array, for example the original array is: {"A", "B", "C", "D"}, then the inverted array is {"D", "C", "B", "a"}. The following implementation method, here for the sake of simplicity I borrowed the list's Add method.

Package Org.webdriver.autotest.study;import Java.util.arraylist;public class Array_examp1 {public static void main ( String[] args) {  //TODO auto-generated method stub  //array rollover. For example: {' A ', ' B ', ' C ', ' d '} becomes {' d ', ' C ', ' B ', ' A '}  string[] array_old={"A", "B", "C", "D"};  Arraylist<string> array_new_list=new arraylist<string> ();  String[] Array_new=new string[array_old.length];  for (int i=1;i<=array_old.length;i++)  {   array_new_list.add (array_old[array_old.length-i]);  }  Array_new=array_new_list.toarray (array_new);  for (int j=0;j<array_new.length;j++)  {      System.out.print ("");     System.out.print (Array_new[j]);}}    

  

Operation Result:

D C B A

Without the use of the list, in fact, not the use of the method of the list, but also very simple, the principle of array inversion is the inverted new array, the first element is equal to the last element of the source array:

Package Org.webdriver.autotest.study;//import Java.util.arraylist;public class Array_examp1 {     //array Inverted public static void Main (string[] args) {  //TODO auto-generated method stub  //array rollover. For example: {' A ', ' B ', ' C ', ' d '} becomes {' d ', ' C ', ' B ', ' A '}  string[] array_old={"A", "B", "C", "D"};  Arraylist<string> array_new_list=new arraylist<string> ();  String[] Array_new=new string[array_old.length];  for (int i=1;i<=array_old.length;i++)  {   //array_new_list.add (array_old[array_old.length-i]);   The first element of the new array element equals the last element of the old element   Array_new[i-1]=array_old[array_old.length-i];  Array_new=array_new_list.toarray (array_new);  for (int j=0;j<array_old.length;j++)  {   System.out.print ("");   System.out.print (Array_new[j]);}}    

  

Method of array flipping (Java implementation)

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.