Move element backward in a List using Collections.rotate() in Java

user3652212

I have an ArrayList as mentioned below (My Code Snippet).

My List elements are in this order initially - ("Periodic", "Multiple", "Single", "Subsequent", "Consecutive").

After applying Collections.rotate() on my List elements the modified List should look like -

("Periodic", "Subsequent", "Multiple", "Single", "Consecutive").

Element "Subsequent" needs to be moved backward to the 1st index element in the List which is "Multiple", so that the element at 1st index will be pushed down to the 2nd index after rotating.

When I tried using Collections.rotate(), it throws an Exception.

IllegalArgumentException "fromIndex > toIndex".

I researched and understood the error, toIndex should always be greater than or equal to fromIndex, but I could not quite figure out how to modify my code snippet in order to achieve what I need.

Any suggestions would greatly be appreciated.

Isn't it possible to move an element backward in a List using rotate() method?

List<String> list = new ArrayList<String>(Arrays.asList("Periodic", "Multiple", "Single", "Subsequent", "Consecutive"));
       for (int i = 0; i < list.size(); i++) {
            int indexOfSubName = 0;
            int indexOfMultipleName = 0;

            String name = list.get(i);

            if (name.equalsIgnoreCase("Subsequent")) {
                indexOfSubName = list.indexOf(name);

            }

            if (name.equalsIgnoreCase("Multiple")) {
                int indexOfMultipleName = list.indexOf(name);
            }
           Collections.rotate(list.subList(indexOfSubName , indexOfMultipleName ), 1);
        }
SMA

Two issues:

  • You are trying to get sublist from 3 to 1 which should be vice versa. So you could check min and max and then try to get sublist.
  • Your sublist call will bring you element say from index 1 to n-1 i.e. if you pass 3, you will get just two elements i.e. index 1 and 2 and not the third one. See this javadoc for details. You could do something like:

    List<String> list = new ArrayList<String>(Arrays.asList("Periodic", "Multiple", "Single", "Subsequent", "Consecutive"));
    int indexOfSubName = 0;
    int indexOfMultipleName = 0;
    for (int i = 0; i < list.size(); i++) {
        String name = list.get(i);
        if (name.equalsIgnoreCase("Subsequent")) {
            indexOfSubName = list.indexOf(name);
        }
        if (name.equalsIgnoreCase("Multiple")) {
            indexOfMultipleName = list.indexOf(name);
        }
    }
    int min = Math.min(indexOfMultipleName, indexOfSubName);
    int max = Math.max(indexOfMultipleName, indexOfSubName);
    Collections.rotate(list.subList(min, max+1), 1);
    System.out.println(list);
    

if you run this, you will get output as what you need:

[Periodic, Subsequent, Multiple, Single, Consecutive]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java Collections to filter element using regular expression

From Dev

Java Collections to filter element using regular expression

From Dev

Rotate and delete an element from a list

From Dev

Dynamically move a div above a list element using Javascript

From Dev

Move one word backward repeatedly using ESC + B

From Dev

Java random shuffle list with two elements using Collections.shuffle

From Dev

Using List.of for immutable list with single element instead of Collections.singletonList

From Dev

Rotate a singly linked list in Java

From Java

Set the percentage of animation in a transition of an element (forward, backward) using a slider

From Dev

Why does methods for wrapping element in Set/List/Map contains 'singleton' in name ?(java.util.Collections)

From Dev

Using Slider to rotate Object In java

From Dev

Using Slider to rotate Object In java

From Dev

text moving out of the element using transform rotate

From Dev

How to rotate element on mousewheel using jquery?

From Dev

Move XML element using XDT

From Dev

Move XML element using XDT

From Dev

Move a DOM element Using jQuery

From Dev

How does traverse backward work for a Doubly Linked List in Java?

From Dev

Java Collections.sort for wildcard list (List<?>)

From Dev

move forward and backward by one word

From Dev

How to move backward parent folder

From Dev

move forward and backward by one word

From Dev

Using volatile collections and arrays in Java

From Dev

Using volatile collections and arrays in Java

From Dev

Move an IWeb Element from one list to another and comparing their count using Selenium ?

From Dev

How do i create a backward triangle in java using for loops

From Dev

Unable to sort list of files where the file name would contain the timestamp using Collections(Java)

From Dev

CSS: Move an unordered list element downwards

From Dev

Move every second element to the back of a list, recursively

Related Related

  1. 1

    Java Collections to filter element using regular expression

  2. 2

    Java Collections to filter element using regular expression

  3. 3

    Rotate and delete an element from a list

  4. 4

    Dynamically move a div above a list element using Javascript

  5. 5

    Move one word backward repeatedly using ESC + B

  6. 6

    Java random shuffle list with two elements using Collections.shuffle

  7. 7

    Using List.of for immutable list with single element instead of Collections.singletonList

  8. 8

    Rotate a singly linked list in Java

  9. 9

    Set the percentage of animation in a transition of an element (forward, backward) using a slider

  10. 10

    Why does methods for wrapping element in Set/List/Map contains 'singleton' in name ?(java.util.Collections)

  11. 11

    Using Slider to rotate Object In java

  12. 12

    Using Slider to rotate Object In java

  13. 13

    text moving out of the element using transform rotate

  14. 14

    How to rotate element on mousewheel using jquery?

  15. 15

    Move XML element using XDT

  16. 16

    Move XML element using XDT

  17. 17

    Move a DOM element Using jQuery

  18. 18

    How does traverse backward work for a Doubly Linked List in Java?

  19. 19

    Java Collections.sort for wildcard list (List<?>)

  20. 20

    move forward and backward by one word

  21. 21

    How to move backward parent folder

  22. 22

    move forward and backward by one word

  23. 23

    Using volatile collections and arrays in Java

  24. 24

    Using volatile collections and arrays in Java

  25. 25

    Move an IWeb Element from one list to another and comparing their count using Selenium ?

  26. 26

    How do i create a backward triangle in java using for loops

  27. 27

    Unable to sort list of files where the file name would contain the timestamp using Collections(Java)

  28. 28

    CSS: Move an unordered list element downwards

  29. 29

    Move every second element to the back of a list, recursively

HotTag

Archive