Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组列表中的移位_Java_Arraylist - Fatal编程技术网

Java 数组列表中的移位

Java 数组列表中的移位,java,arraylist,Java,Arraylist,方法publicstaticarraylistrotate(arraylistal,int-shift)接受字符串的ArrayList(至少在本例中是这样)和shift,后者指示了ArrayList应该移位多少。如果我有,让我们说一个 [ A, B, C, D, E, F, G] 移位为2,因此该方法返回 [ F, G, A, B, C, D, E] [ D, E, F, G, A, B, C] 再比如, [ A, B, C, D, E, F, G] 而shift为4,则该方法返回

方法
publicstaticarraylistrotate(arraylistal,int-shift)
接受字符串的
ArrayList
(至少在本例中是这样)和
shift
,后者指示了ArrayList应该移位多少。如果我有,让我们说一个

[ A, B, C, D, E, F, G] 
移位为
2
,因此该方法返回

[ F, G, A, B, C, D, E]
[ D, E, F, G, A, B, C] 
再比如,

[ A, B, C, D, E, F, G]
shift
4
,则该方法返回

[ F, G, A, B, C, D, E]
[ 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;

  }

}
import java.util.ArrayList;
公课轮换{
//演示旋转(移动)方法
公共静态void main(字符串arg[]){
ArrayList charsL;
charsL=newarraylist();
char[]chars={A',B',C',
‘D’、‘E’、‘F’};
for(char c:chars){
添加(c)项;
}
//charsL=[A,B,C,D,E,F]
ArrayList结果1;
结果1=旋转(charsL,2);
//结果1==[E,F,A,B,C,D]
系统输出打印项次(结果1);
结果1=旋转(charsL,7);
//结果1==[F,A,B,C,D,E]
系统输出打印项次(结果1);
//也适用于SRTINGS
ArrayList stringL;
stringL=新的ArrayList();
字符串[]strs={“A”、“B”、“C”,
“D”、“E”、“F”、“G”};
用于(字符串s:strs){
stringL.添加(s);
}
//stringL=[A,B,C,D,E,F,G]
ArrayList结果2;
结果2=旋转(stringL,7);
//结果2==[A、B、C、D、E、F、G]
系统输出打印项次(结果2);
结果2=旋转(stringL,4);
//结果2==[D,E,F,G,A,B,C]
系统输出打印项次(结果2);
}
公共静电
ArrayList旋转(ArrayList aL,int shift){
//你的定义在这里
ArrayList newValues=新的ArrayList();
ArrayList temp=新的ArrayList();
对于(int i=0;i
不清楚您为什么一直
调用aL.get(shift)
以及为什么一直从只应添加到的列表中删除

尝试:

publicstaticarraylistrotate(arraylistl,int-shift){
左列表=l.子列表(0,aL.size()-shift);
列表右=l.子列表(aL.size()-shift,l.size());
List res=新阵列列表(右);
res.addAll(左);
返回res;
}
试试这个:

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;
}
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;
}
}
publicstaticarraylistrotate(arraylistal,int-shift)
{
如果(aL.size()==0)
返回aL;
T元素=null;
for(int i=0;i
如果您可以使用内置解决方案,那么您的代码可以像

public static <T> List<T> rotate(List<T> aL, int shift) {
    List<T> newValues = new ArrayList<>(aL);
    Collections.rotate(newValues, shift);
    return newValues;
}
公共静态列表旋转(列表aL、int-shift){
List newValues=newarraylist(aL);
集合。旋转(newValues、shift);
返回新值;
}
试试这个:

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;
}
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;
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共级明矾{
公共静态数组列表旋转(列表aL,整型移位){
ArrayList newValues=新的ArrayList(aL);
集合。旋转(newValues、shift);
返回新值;
}
}
测试用例:

// 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);
  }
}
//明矾的公共测试
导入org.junit.*;
导入静态org.junit.Assert.*;
导入java.util.*;
导入java.io.*;
公营校友会{
/*Main方法在此文件中运行测试*/
公共静态void main(字符串参数[]){
org.junit.runner.JUnitCore.main(“测试”);
} 
public void checkRotate(td[],int-shift,字符串){
ArrayList a=新的ArrayList();
a、 addAll(Arrays.asList(d));
字符串sourceBefore=a.toString();
ArrayList actualA=ALUtils.旋转(a,shift);
字符串sourceAfter=a.toString();
字符串实际值=actualA.toString();
串味精=
字符串格式(“旋转不正确\n”)+
String.format(“源:%s(旋转前)\n”,源之前)+
格式(“班次:%d\n”,班次)+
格式(“预期:%s\n”,预期)+
格式(“实际值:%s\n”,实际值)+
格式(“源:%s(旋转后)\n”,sourceAfter);
资产质量(信息、预期、实际);
assertEquals(msg、sourceBefore、sourceAfter);
}
@测试公共空隙测试1(){
字符[]d={A',B',C',d',E',F'};
int-shift=2;
字符串预期为=“[E、F、A、B、C、D]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试2(){
字符[]d={A',B',C',d',E',F'};
int-shift=7;
String expected=“[F,A,B,C,D,E]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试3(){
字符串[]d={“A”、“B”、“C”、“d”、“E”、“F”、“G”};
int-shift=7;
字符串预期为=“[A、B、C、D、E、F、G]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试4(){
字符串[]d={“A”、“B”、“C”、“d”、“E”、“F”、“G”};
int-shift=4;
字符串预期为=“[D、E、F、G、A、B、C]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试5(){
整数[]d={1};
int-shift=4;
字符串应为=“[1]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试6(){
整数[]d={};
int-shift=2;
字符串应为=“[]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试_rotate7(){
字符串[]d={“A”、“B”、“C”、“d”、“E”、“F”、“G”};
int-shift=0;
字符串预期为=“[A、B、C、D、E、F、G]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试_rotate8(){
字符串[]d={“A”、“B”、“C”、“d”、“E”、“F”、“G”};
int-shift=28;
字符串预期为=“[A、B、C、D、E、F、G]”;
选中旋转(d、shift、预期);
}
@测试公共空隙测试_rotate9(){
字符串[]d={“A”、“B”、“C”、“d”、“E”、“F”、“G”};
在里面