【问题标题】:Shifting in arrayList在arrayList中移动
【发布时间】:2015-04-09 20:37:05
【问题描述】:

public static <T> ArrayList<T> rotate(ArrayList<T> aL, int shift) 方法接受 String 的 Arraylist(至少在这个例子中)和一个 shift,它指示数组列表应该移动多少。如果我有,让我们说一个 arayList 的

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

而移位是2,所以方法返回

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

或者另一个例子,

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

shift4,则方法返回

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

我的方法完全错误,不知道如何解决这个问题。 smb 可以帮我吗?

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;

  }

}

【问题讨论】:

  • 你能解释(或更正)你的第一个例子吗?为什么列表的大小发生了变化?你在FA之间错过了G吗?
  • 我对轮班规则有点不清楚。您是否打算让 4 的移位旋转数据,但 2 的移位既旋转又截断它?
  • 在不考虑旋转算法的情况下,我可以告诉你这一行:newValues.remove(aL.get(shift)); 正在尝试从空的List 中删除值,并且会失败。

标签: java arraylist


【解决方案1】:

如果你可以使用内置的解决方案,那么你的代码可以很简单

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 它可以工作,但我不明白它是如何工作的!你能解释一下吗? @约翰H
    • 基本上,E remove(int index) 方法从列表中移除一个元素并返回移除的元素。然后我们使用方法public void add(int index, E element) 将元素添加到ArrayList 的第一个索引中。您可能会发现此链接很有帮助:docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
    【解决方案3】:

    试试这个:

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

    测试用例:

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

    【讨论】:

      【解决方案4】:

      目前还不清楚为什么要保留calling aL.get(shift),以及为什么要不断从只应添加的列表中删除。

      试试:

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

      【讨论】:

        猜你喜欢
        • 2016-06-14
        • 1970-01-01
        • 2021-02-27
        • 1970-01-01
        • 1970-01-01
        • 2014-03-19
        • 2010-10-06
        • 2010-12-20
        相关资源
        最近更新 更多