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

Rotate Number or String, one char or one digit at a time.

 Rotate given string or number in Java:

Solution 1 of 2:

import java.util.Arrays;

 

public class Test {

 

     public static void main(String[] args) {

 

         String word = "beautiful";

         int len = word.length();

         String[] str = new String[len];

         String x ;

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

              str[i] = Character.toString(word.charAt(i));

         }

         System.out.println("The array values : "+Arrays.toString(str));

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

              x=str[0];

         for(int i=0;i<len-1; i++) {

             

                  str[i]=str[i+1];

         }

         str[len-1]=x;

                  System.out.println("The array values : "+Arrays.toString(str));

         }       

     }

}

 

Output:

The array values : [b, e, a, u, t, i, f, u, l]

The array values : [e, a, u, t, i, f, u, l, b]

The array values : [a, u, t, i, f, u, l, b, e]

The array values : [u, t, i, f, u, l, b, e, a]

The array values : [t, i, f, u, l, b, e, a, u]

The array values : [i, f, u, l, b, e, a, u, t]

The array values : [f, u, l, b, e, a, u, t, i]

The array values : [u, l, b, e, a, u, t, i, f]

The array values : [l, b, e, a, u, t, i, f, u]

The array values : [b, e, a, u, t, i, f, u, l]

 

Solution 2 of 2 :

Above Solution covers every rotating string or number of one char or one digit one time.

Below solution is specific to the problem as shown below, and it used replace in ArrayList as well as removeAll in ArrayList.

import java.util.ArrayList;

public class Test {

     public static void main(String[] args) {

         /*

         1000

         0100

         0010

         0001

         */

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

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

         int count = 0 ;

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

              int a=0,b=0,c=0,d=0;

              alist.add(a);

              alist.add(b);

              alist.add(c);

              alist.add(d);

              blist = alist ;

              alist.set(count,1);//Replace ArrayList element, you should give index value of arraylist here

              System.out.println("The Arraylist values : "+alist);

              alist.removeAll(blist);

                       count ++ ;

         }    

     }

}

Output:

The Arraylist values : [1, 0, 0, 0]
The Arraylist values : [0, 1, 0, 0]
The Arraylist values : [0, 0, 1, 0]
The Arraylist values : [0, 0, 0, 1]

 

No comments:

Post a Comment