Java Program to Rotate Elements of a List

JavaCampus InterviewServer Side ProgrammingProgramming

In this article, we will understand how to rotate elements of a list. The List extends Collection and declares the behavior of a collection that stores a sequence of elements. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

Below is a demonstration of the same −

Suppose our input is

Input list: [100, 150, 200, 250, 300]

The desired output would be

The list after one rotation: [150, 200, 250, 300, 100]

Algorithm

Step 1 - START
Step 2 - Declare a list namely input_list
Step 3 - Define the values.
Step 4 - Iterate through the list, and use the ‘get’ method to get the element at a specific index.
Step 5 - Assign this variable to a new variable ‘temp’.
Step 6 - Iterate through the list from the end, and fetch the element at a specific index. Use the ‘set’ method to set the value at ‘temp’.
Step 7 - Display the result
Step 8 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

import java.util.*;
public class Demo {
   public static void main(String[] args){
      List<Integer> input_list = new ArrayList<>();
      input_list.add(100);
      input_list.add(150);
      input_list.add(200);
      input_list.add(250);
      input_list.add(300);
      System.out.println("The list is defined as: " + Arrays.toString(input_list.toArray()));
      for (int i = 0; i < 4; i++) {
         int temp = input_list.get(4);
         for (int j = 4; j > 0; j--) {
            input_list.set(j, input_list.get(j - 1));
         }
         input_list.set(0, temp);
      }
      System.out.println( "The list after one rotation: " + Arrays.toString(input_list.toArray()));
   }
}

Output

The list is defined as: [100, 150, 200, 250, 300]
The list after one rotation: [150, 200, 250, 300, 100]

Example 2

Here, we encapsulate the operations into functions exhibiting object oriented programming.

import java.util.*;
public class Demo {
   static void rotate(List<Integer> input_list){
      for (int i = 0; i < 4; i++) {
      int temp = input_list.get(4);
      for (int j = 4; j > 0; j--) {
         input_list.set(j, input_list.get(j - 1));
      }
      input_list.set(0, temp);
   }
   System.out.println("
The list after one rotation: " + Arrays.toString(input_list.toArray()));    }    public static void main(String[] args){       List<Integer> input_list = new ArrayList<>();       input_list.add(100);       input_list.add(150);       input_list.add(200);       input_list.add(250);       input_list.add(300);       System.out.println("The list is defined as: " + Arrays.toString(input_list.toArray()));       rotate(input_list);    } }

Output

The list is defined as: [100, 150, 200, 250, 300]

The list after one rotation: [150, 200, 250, 300, 100]
raja
Updated on 30-Mar-2022 07:24:09

Advertisements