Java output A string in reverse order recursively

Source: Internet
Author: User

Recently found such a topic, how to use the string recursive way output:

As the string "Hello World my friend and now"--〉now and friend my world Hello

To achieve this method a lot, my method may be inefficient, if there is a better way please advise.

public static void Main (string[] args) {System.out.println (Reserve (""));} public static string reserve (String str) {string objstr= ' Hello world my friend and now '; object[] Array=objstr.split (""); Ob Ject[] Nowarry=str.split (""); Array=compare (array, nowarry); if (array.length<1) {return str;} for (int i=array.length-1;i>=0;i--) {str=str+ "" "+array[i];reserve (str);} return str;} private static object[] Compare (object[] array,object[] nowarry) {list<object> endlist=new arraylist<object > (); List<object> ownlist=new arraylist<object> (); for (int. i=0;i<array.length;i++) {for (int j=0;j< nowarry.length;j++) {if (Array[i].equals (Nowarry[j])) {Ownlist.add (array[i]);}} Endlist.add (Array[i]);} Endlist.removeall (ownlist); return  Endlist.toarray ();}

[Result show]: now and friend my world Hello

It is important to note that the classcastexception exception occurs when the Endlist.toarray () conversion is performed in the system when string[is used to identify the array. So replace all of the above strings with object objects.


If there is a simpler way to implement, please advise. Thank you

Java output A string in reverse order recursively

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.