http://www.technicalpage.net/search/label/SQL

ReverseOrder

Put number(or String) in Reverse order 

To watch in YouTube: click here

package Test;

import java.util.ArrayList;

 

public class ReverseOrder { //reverse order

 

       public static void main(String[] args) {

            

             int numb = 1234567 ;

             String numbStr = Integer.toString(numb);

             int lengthNum = numbStr.length();

            

             ArrayList<String> alist = new ArrayList<>();

             ArrayList<String> blist = new ArrayList<>();

             for(int i=0;i<lengthNum; i++) {

                    String alistString = Character.toString(numbStr.charAt(i));

                    alist.add(alistString);

             }

            

             System.out.println("alist is "+alist); //[1, 2, 3, 4, 5, 6, 7]

            

             for(int x=lengthNum-1;x>=0;x--) {

                    String strAtx = alist.get(x);

                    blist.add(strAtx);

             }

            

             System.out.println("blist is "+blist);//[7, 6, 5, 4, 3, 2, 1]

            

             String finalStr = "" ; //This should not be null

             for(int j=0; j<lengthNum; j++ ) {

                    finalStr = finalStr.concat(blist.get(j));

             }

            

             int finalNumb = Integer.parseInt(finalStr);

             System.out.println("The number in reverse order is "+finalNumb); //7654321

       }

 

}

 

Output:

alist is [1, 2, 3, 4, 5, 6, 7]

blist is [7, 6, 5, 4, 3, 2, 1]

The number in reverse order is 7654321

No comments:

Post a Comment