8

The method public static <T> ArrayList<T> rotate(ArrayList<T> aL, int shift) accepts an Arraylist of String (at least in this example) and a shift which indicated how much the arraylist should shift. If I have, let's say an arayList of

[ A, B, C, D, E, F, G] 

and the shift is 2, so the method returns

[ F, G, A, B, C, D, E]

or another example,

[ A, B, C, D, E, F, G]

and shift is 4, then the method returns

[ D, E, F, G, A, B, C] 

I did the method completely wrong and have no clue how to approach this problem. Could smb help me with that ?

import java.util.ArrayList;
public class Rotation{
  // Demonstrate rotat(a,shift) method
  public static void main(String arg[]){
    ArrayList<Character> charsL;
    charsL = new ArrayList<Character>();
    char [] chars = { 'A', 'B', 'C',
                      'D', 'E', 'F'};
    for(char c : chars){
      charsL.add(c);
    }
    // charsL = [ A, B, C, D, E, F]

    ArrayList<Character> result1;

    result1 = rotate(charsL, 2);
    // result1== [ E, F, A, B, C, D]
    System.out.println(result1);
    
    result1 = rotate(charsL, 7);
    // result1== [ F, A, B, C, D, E]
    System.out.println(result1);

    // WORKS WITH SRTINGS TOO
    ArrayList<String> stringL;
    stringL = new ArrayList<String>();
    String [] strs = { "A", "B", "C",
                       "D", "E", "F", "G" };
    for(String s : strs){
      stringL.add(s);
    }
    // stringL = [ A, B, C, D, E, F, G]

    ArrayList<String> result2;

    result2 = rotate(stringL, 7);
    // result2== [ A, B, C, D, E, F, G]
    System.out.println(result2);

    result2 = rotate(stringL, 4);
    // result2== [ D, E, F, G, A, B, C]
    System.out.println(result2);
  }

  public static <T>
  ArrayList<T> rotate(ArrayList<T> aL, int shift){
    // YOUR DEFINITION HERE 
      
      ArrayList <T> newValues = new ArrayList<>();
      ArrayList <T> temp = new ArrayList<>();
      
      for(int i = 0; i < aL.size(); i++)
      {
          newValues.remove(aL.get(shift));
          newValues.add(aL.get(i));
         //newValues.add(shift, aL.get(i));
          
      }
      return newValues;

  }

}
4
  • 1
    Can you explain (or correct) your first example? Why size of list changed? Did you miss G between F and A? Commented Apr 9, 2015 at 20:43
  • 1
    I'm a little unclear about the shift rule. Did you intend that a shift of 4 rotates the data, but a shift of 2 both rotates and truncates it? Commented Apr 9, 2015 at 20:44
  • Without giving any though to a rotation algorithm, I can tell you that this line: newValues.remove(aL.get(shift)); is trying to remove values from an empty List, and is going to fail. Commented Apr 9, 2015 at 20:48
  • I always use java.util.Stack for this purpose. Commented Jan 28 at 12:52

4 Answers 4

16

If you can use built-in solution then you can use Collections.rotate(List<?> list, int distance).

BTW when value of distance is

  • positive it rotates element to the right (example: distance=1 [a, b, c] -> [c, a, b])
  • negative it rotates elements to the left (example: distance=-1 [a, b, c] -> [b, c, a])

Anyway in your case it looks like your code can be simplified to

public static <T> List<T> rotate(List<T> aL, int shift) {
    List<T> newValues = new ArrayList<>(aL);
    Collections.rotate(newValues, shift);
    return newValues;
}
Sign up to request clarification or add additional context in comments.

Comments

6

Try this:

public static <T> ArrayList<T> rotate(ArrayList<T> aL, int shift)
{
    if (aL.size() == 0)
        return aL;

    T element = null;
    for(int i = 0; i < shift; i++)
    {
        // remove last element, add it to front of the ArrayList
        element = aL.remove( aL.size() - 1 );
        aL.add(0, element);
    }

    return aL;
}

2 Comments

It works but I dont get how it works! Could you please explain ? @JohnH
Basically, the method E remove(int index) removes an element from the list and returns the removed element. Then we use the method public void add(int index, E element) to add the element to the very first index of the ArrayList. You may find this link helpful: docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
2

Try this:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ALUtils {

public static <T> ArrayList<T> rotate(List<T> aL, int shift) {
    ArrayList<T> newValues = new ArrayList<>(aL);
    Collections.rotate(newValues, shift);
    return newValues;
}
}

Test Cases:

// Public tests for ALUtils

import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import java.io.*;

public class ALUtilsTests {
  /*Main method runs tests in this file*/ 
  public static void main(String args[]) {
    org.junit.runner.JUnitCore.main("Tests");
  } 

  public <T> void checkRotate(T d[], int shift, String expectS){
    ArrayList<T> a = new ArrayList<T>();
    a.addAll(Arrays.asList(d));
    String sourceBefore = a.toString();

    ArrayList<T> actualA = ALUtils.rotate(a, shift);

    String sourceAfter = a.toString();
    String actualS = actualA.toString();
    String msg =
      String.format("Rotation incorrect\n")+
      String.format("Source  : %s (before rotate)\n",sourceBefore)+
      String.format("Shift   : %d\n",shift)+
      String.format("Expect  : %s\n",expectS)+
      String.format("Actual  : %s\n",actualS)+
      String.format("Source  : %s (after rotate)\n",sourceAfter);
    assertEquals(msg,expectS,actualS);
    assertEquals(msg,sourceBefore,sourceAfter);
  }

  @Test public void test_rotate1() {
    Character [] d = { 'A', 'B', 'C', 'D', 'E', 'F'};
    int shift = 2;
    String expectS = "[E, F, A, B, C, D]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate2() {
    Character [] d = { 'A', 'B', 'C', 'D', 'E', 'F'};
    int shift = 7;
    String expectS = "[F, A, B, C, D, E]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate3() {
    String [] d =  { "A", "B", "C", "D", "E", "F", "G" };
    int shift = 7;
    String expectS = "[A, B, C, D, E, F, G]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate4() {
    String [] d =  { "A", "B", "C", "D", "E", "F", "G" };
    int shift = 4;
    String expectS = "[D, E, F, G, A, B, C]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate5() {
    Integer [] d =  { 1};
    int shift = 4;
    String expectS = "[1]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate6() {
    Integer [] d =  {};
    int shift = 2;
    String expectS = "[]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate7() {
    String [] d =  { "A", "B", "C", "D", "E", "F", "G" };
    int shift = 0;
    String expectS = "[A, B, C, D, E, F, G]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate8() {
    String [] d =  { "A", "B", "C", "D", "E", "F", "G" };
    int shift = 28;
    String expectS = "[A, B, C, D, E, F, G]";
    checkRotate(d,shift,expectS);
  }

  @Test public void test_rotate9() {
    String [] d =  { "A", "B", "C", "D", "E", "F", "G" };
    int shift = 39;
    String expectS = "[D, E, F, G, A, B, C]";
    checkRotate(d,shift,expectS);
  }
}

Comments

1

It is very unclear why you keep calling aL.get(shift) and why you keep removing from the list you should only be adding to.

Try:

public static <T> ArrayList<T> rotate(ArrayList<T> l, int shift) {
    List<T> left = l.subList(0, aL.size() - shift);
    List<T> right = l.subList(aL.size() - shift, l.size());
    List<T> res = new ArrayList<T>(right);
    res.addAll(left);
    return res;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.