@Test public void testRightShifts() { Object[] innerArray = new Object[] { "A", "B", "C", "D", "E", "a", "b", "c" }; list.values = innerArray; list.arrayLength = innerArray.length; list.shift(0, 5, 1); assertInnerArrayWithNullFlip("_ABCDEbc"); list.shift(1, 6, 1); assertInnerArrayWithNullFlip("__ABCDEc"); list.shift(2, 7, 1); assertInnerArrayWithNullFlip("___ABCDE"); innerArray[0] = "a"; innerArray[1] = "b"; innerArray[2] = "c"; assertInnerArrayWithNullFlip("abcABCDE"); list.shift(3, 0, 1); assertInnerArrayWithNullFlip("Ebc_ABCD"); list.shift(4, 1, 1); assertInnerArrayWithNullFlip("DEc__ABC"); list.shift(5, 2, 1); assertInnerArrayWithNullFlip("CDE___AB"); innerArray[3] = "a"; innerArray[4] = "b"; innerArray[5] = "c"; assertInnerArrayWithNullFlip("CDEabcAB"); list.shift(6, 3, 1); assertInnerArrayWithNullFlip("BCDEbc_A"); list.shift(7, 4, 1); assertInnerArrayWithNullFlip("ABCDEc__"); list.shift(0, 5, 1); assertInnerArrayWithNullFlip("_ABCDE__"); }
void shift(int first, int last, int distance) { if (distance != 1 && distance != -1) { throw new IllegalArgumentException(); } if (first == last) { return; } if (last == 0) { last = arrayLength; } if (first > last && last != 0) { if (distance == 1) { System.arraycopy(values, 0, values, 1, last); values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, arrayLength - first - 1); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, arrayLength - first); values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } } else { if (distance == -1 && first == 0) { values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } else if (distance == 1 && last == arrayLength) { values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, last - first - 1); values[first] = null; } else if(distance == 1) { System.arraycopy(values, first, values, first + 1, last - first); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, last - first); values[last - 1] = null; } } }
CircularArrayList extends AbstractList<T> { void shift(int first, int last, int distance) { if (distance != 1 && distance != -1) { throw new IllegalArgumentException(); } if (first == last) { return; } if (last == 0) { last = arrayLength; } if (first > last && last != 0) { if (distance == 1) { System.arraycopy(values, 0, values, 1, last); values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, arrayLength - first - 1); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, arrayLength - first); values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } } else { if (distance == -1 && first == 0) { values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } else if (distance == 1 && last == arrayLength) { values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, last - first - 1); values[first] = null; } else if(distance == 1) { System.arraycopy(values, first, values, first + 1, last - first); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, last - first); values[last - 1] = null; } } } }
CircularArrayList extends AbstractList<T> { void shift(int first, int last, int distance) { if (distance != 1 && distance != -1) { throw new IllegalArgumentException(); } if (first == last) { return; } if (last == 0) { last = arrayLength; } if (first > last && last != 0) { if (distance == 1) { System.arraycopy(values, 0, values, 1, last); values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, arrayLength - first - 1); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, arrayLength - first); values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } } else { if (distance == -1 && first == 0) { values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } else if (distance == 1 && last == arrayLength) { values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, last - first - 1); values[first] = null; } else if(distance == 1) { System.arraycopy(values, first, values, first + 1, last - first); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, last - first); values[last - 1] = null; } } }  }
CircularArrayList extends AbstractList<T> { void shift(int first, int last, int distance) { if (distance != 1 && distance != -1) { throw new IllegalArgumentException(); } if (first == last) { return; } if (last == 0) { last = arrayLength; } if (first > last && last != 0) { if (distance == 1) { System.arraycopy(values, 0, values, 1, last); values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, arrayLength - first - 1); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, arrayLength - first); values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } } else { if (distance == -1 && first == 0) { values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } else if (distance == 1 && last == arrayLength) { values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, last - first - 1); values[first] = null; } else if(distance == 1) { System.arraycopy(values, first, values, first + 1, last - first); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, last - first); values[last - 1] = null; } } }  @Override T get(int index); @Override void add(int index, T element); @Override T remove(int index); @Override int size(); }
CircularArrayList extends AbstractList<T> { void shift(int first, int last, int distance) { if (distance != 1 && distance != -1) { throw new IllegalArgumentException(); } if (first == last) { return; } if (last == 0) { last = arrayLength; } if (first > last && last != 0) { if (distance == 1) { System.arraycopy(values, 0, values, 1, last); values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, arrayLength - first - 1); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, arrayLength - first); values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } } else { if (distance == -1 && first == 0) { values[arrayLength - 1] = values[0]; System.arraycopy(values, 1, values, 0, last - 1); values[last - 1] = null; } else if (distance == 1 && last == arrayLength) { values[0] = values[arrayLength - 1]; System.arraycopy(values, first, values, first + 1, last - first - 1); values[first] = null; } else if(distance == 1) { System.arraycopy(values, first, values, first + 1, last - first); values[first] = null; } else if(distance == -1) { System.arraycopy(values, first, values, first - 1, last - first); values[last - 1] = null; } } }  @Override T get(int index); @Override void add(int index, T element); @Override T remove(int index); @Override int size();  }