In: Computer Science
java/netbeans
All the explanations is given in the comments of the code itself.
Code--
import java.util.*;
public class Reverse
{
//method to reverse a string
public static String reverseStr(String st)
{
//base condition for
recursion
if(st.isEmpty())
return st;
else
{
//remove the
first character of the string
//concat it with
the returned string
//and pass the
string again recursively without the
//first
character
return
reverseStr(st.substring(1))+st.charAt(0);
}
}
//method to reverse Arraylist
public static ArrayList<String>
reverseAL(ArrayList<String> al)
{
if(al.size()>1)
{
//remove the
first node of the al
String
temp=al.remove(0);
//then again
call reverseAL
reverseAL(al);
//add the temp
to al
al.add(temp);
}
//base condition for recursion that
is if the al is empty
return al;
}
//main method
public static void main(String[] args)
{
Scanner sc=new
Scanner(System.in);
ArrayList<String> al = new
ArrayList<String>();
String temp;
int size=0;
//prompt the user to give inputs
until Done is entered
System.out.println("Enter Inputs
strings: ");
while(true)
{
temp=sc.nextLine();
if(temp.equals("Done"))
{
break;
}
else
{
al.add(temp);
size++;
}
}
//iterate over the array list and
reverse the strings using the above function
for(int i=0;i<size;i++)
{
temp=reverseStr(al.get(i));
al.set(i,
temp);
}
//call the function to reverse the
arraylist
al=reverseAL(al);
//display the output after
reversals
System.out.println("Output");
for(int i=0;i<size;i++)
{
System.out.println(al.get(i));
}
}
}
Code Screenshot--
Output Screenshot--
Note--
Please upvote if you like the effort.