Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1032111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:02:00+00:00 2026-05-16T14:02:00+00:00

I have a List containing elements that are accessed according to their indexes. In

  • 0

I have a List containing elements that are accessed according to their indexes. In this list I need to be able to “rotate” groups of 4 elements according to their index. For example in the list

[a, b, c, d, e ,f , g, h, i, j, k, l]

I want to rotate c, f, i, l in order to get

[a, b, l, d, e ,c , g, h, f, j, k, i]

How will you implement this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:02:01+00:00Added an answer on May 16, 2026 at 2:02 pm

    A straightforward solution

    If you only need to 1-rotate elements at 4 indices of a List, you can just write a straightforward generic method like this:

    static <T> void rotate4(List<T> list, int i0, int i1, int i2, int i3) {
        T item = list.get(i3);
        item = list.set(i0, item);
        item = list.set(i1, item);
        item = list.set(i2, item);
        item = list.set(i3, item);
    }
    

    This will cyclically rotate 4 elements of any List<T>. Remember that List.set returns the element that previously was at that index, so you could write the entire method in one-line if you want:

        // one-liner version
        list.set(i3, list.set(i2, list.set(i1, list.set(i0, list.get(i3)))));
    

    With this helper method, you’ll have:

        List<Character> list = Arrays.asList(
            'a','b','c','d','e','f','g','h','i','j','k','l'
        );
    
        System.out.println(list);
        // [a, b, c, d, e, f, g, h, i, j, k, l]
        //        *        *        *        *
    
        rotate4(list, 2, 5, 8, 11);
    
        System.out.println(list);       
        // [a, b, l, d, e, c, g, h, f, j, k, i]
        //        *        *        *        *
    

    A more general solution

    IF you need a way to rotate an arbitrary number of elements for an arbitrary distance, then you can create a live view of another List, and then you can Collections.rotate that view.

    IF the elements are consecutive, for example, you’d just use subList:

        List<Character> list = Arrays.asList(
            'a','b','c','d','e','f','g','h','i','j','k','l'
        );
    
        System.out.println(list);
        // [a, b, c, d, e, f, g, h, i, j, k, l]
        //     *  *  *  *  *
    
        System.out.println(list.subList(1, 6));
        // [b, c, d, e, f]
    
        Collections.rotate(list.subList(1, 6), -2);
        System.out.println(list);
        // [a, d, e, f, b, c, g, h, i, j, k, l]
        //     *  *  *  *  *
    

    Since the elements aren’t consecutive, you can’t use subList, but you can write e.g. PeriodicalLiveViewList class. You want to be able to write something like this:

        System.out.println(PeriodicalLiveViewList.of(list, 3, 2));
        // [c, f, i, l]
    
        Collections.rotate(PeriodicalLiveViewList.of(list, 3, 2), 1);
    

    Basically you create another List whose elements are every 3rd element of another List, starting at index 2, as a live view.

    If you are using Guava, there is ForwardingList that you can built on. You can implement the decorator pattern for this from scratch too if necessary.

    Related questions

    • Guava ForwardingList usage example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List containing several keywords. I foreach through them building my linq
I have a list of objects, each containing an Id, Code and Description. I
I have a list of jar files, all containing slightly different versions of the
I have div containing a list of flash objects. The list is long so
I have an Excel spreadsheet containing a list of strings. Each string is made
I have a simple accordion type page containing a list of H3 headers and
I have two tables containing Tasks and Notes, and want to retrieve a list
Lets say I have 2 lists, containing elements: values uncertainties of the values Values
Let's assume we have a java.util.LinkedList containing a few hundreds of elements. One possibility
If I have List<String> text how can I create a sub-list of all continious

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.