Questions about string inversion and array inversion

Source: Internet
Author: User

About the reversal of the problem, in the evening to check some information, there are many very good ideas, to master these ideas skillfully, now summarize these as follows:

String inversion:

1 ImportJava.util.Stack;2 3  Public classStringinverse {4      Public Static voidMain (string[] args) {5System.out.println (Reverse7 ("ABCDE"));6     }7     //Way One8      Public Staticstring Reverse1 (string s) {9         intLength =s.length ();Ten         if(Length <= 1) One             returns; AString left = s.substring (0, LENGTH/2);//AB -String right = s.substring (length/2, length);//de -         returnReverse1 (right) + reverse1 (left);//recursive!!! are used here the     } -     //Mode two -      Public Staticstring Reverse2 (string s) { -         intLength =s.length (); +String reverse = "";//empty string -          for(inti = 0; i < length; i++) +Reverse = S.charat (i) + reverse;//reverse, put it in the back . A         //charAt () returns a char at         returnreverse; -     } -     //Way Three -      Public Staticstring Reverse3 (string s) { -         Char[] Array =S.tochararray (); -String reverse = "";//empty string in          for(inti = array.length-1; I >= 0; i--) -Reverse + =Array[i]; to         //reverse put it in front . +         returnreverse; -     } the     //Mode four *      Public Staticstring Reverse4 (string s) { $         //JDK-provided APIPanax Notoginseng         return NewStringBuffer (s). Reverse (). toString (); -     } the     //Mode five +      Public Staticstring Reverse5 (String str) { A         //The string is programmed into a character array, and the position of the character is replaced by the subscript of the array. the         Char[] s =Str.tochararray (); +         //The return value is char[] ToCharArray () converts this string to a new character array.  -         intn = s.length-1; $         intHalflength = N/2; $          for(inti = 0; I <= halflength; i++) { -             Chartemp =S[i]; -S[i] = s[n-i]; theS[n-i] =temp; -         }Wuyi         //use String (char[]) to construct this method. the         return NewString (s); -     } Wu     //Mode six -      Public Staticstring Reverse6 (string s) { About         Char[] str =S.tochararray (); $         intBegin = 0; -         intEnd = S.length ()-1; -          while(Begin <end) { -             //Equal to or two times the same number or string. The next operation is to swap positions for Str[begin] and Str[end]. AStr[begin] = (Char) (Str[begin] ^str[end]); +Str[end] = (Char) (Str[begin] ^str[end]); theStr[begin] = (Char) (Str[end] ^Str[begin]); -begin++; $end--; the         } the         return NewString (str); the     } the     //Way Seven -      Public Staticstring Reverse7 (string s) { in         Char[] str =S.tochararray (); thestack<character> stack =NewStack<character>(); the          for(inti = 0; i < str.length; i++) About Stack.push (Str[i]); theString reversed = ""; the          for(inti = 0; i < str.length; i++) theReversed + =Stack.pop (); +         //pop () removes the object at the top of the stack and returns the object as the value of this function.  -         returnreversed; the     }Bayi}

The inverse of the array (I'll find a more classic):

Array conversion position on the tall .....

1 ImportJava.lang.reflect.Array;2 3  Public classArrayinverse {4 5      Public Static voidMain (string[] args) {6         int[] myarray = {1, 2, 3 };7         int[] NewArray =Invertarray (myarray);8 9          for(inti = 0; i < newarray.length; i++) {TenSystem.out.print (Newarray[i] + ""); One         } A     } -  -     /** the * Inverse Array -      *  -      */ -      Public Static<T>t Invertarray (t array) { +         intLen =array.getlength (Array); -         //gets the type of the generic (both the type of the array) +Class<?> CLASSZ =Array.getclass (). Getcomponenttype (); A         //newinstance (class<?> componenttype,int length) at         //creates a new array with the specified component type and length. Calling this method is equivalent to creating the following array: -Object dest =array.newinstance (CLASSZ, Len); -  -         //arraycopy () copies an array from the specified source array, starting at the specified position and ending at the specified position in the destination array.  -System.arraycopy (array, 0, dest, 0, Len); -  in Object temp; -  to          for(inti = 0; I < (LEN/2); i++) { +temp = Array.get (dest, i);//get (object array, int index) returns the value of the index component in the specified Array object.  -Array.set (dest, I, Array.get (dest, Len-i-1)); theArray.set (dest, Len-i-1, temp); *         } $         return(T) dest;Panax Notoginseng     } -}

Questions about string inversion and array inversion

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.