Shifting in arrayList

If you can use built-in solution then your code can be as simple as

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

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;
}

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);
  }
}

Tags:

Java

Arraylist