【问题标题】:Java: moving items in arrayJava:移动数组中的项目
【发布时间】:2011-04-04 08:01:25
【问题描述】:

我正在寻找在数组中移动东西。

我希望能够将给定数组中的最后一项移动到某个位置,同时将当前位置中的那些项移动到右侧。我希望它从第一个位置移动到第二个位置,依此类推,而不替换当前存在的项目。

例如)

a,b,c,d,e

假设我想移动到“3” - 然后它会变成

a,b,c,e,d

我目前有以下:

public static void moveLastup(String[] stuff, int position) 
{
    String y = stuff[stuff.length-1];

    for (int x = stuff.length-1; x > position; x--) 
        list[x] = list[x-1];

    stuff[position] = y;
}

编辑:对不起,我认为我不够清楚。我想要做的是给定这种方法,我应该能够将最后一块移动到任何地方。

for (int pos = 0; pos < stuff.length; pos++)
{
    moveLastup(list,pos);
    showList(list);
}

现在,当我执行此操作时,它只需获取 for 循环中下一个列表中的最后一项 例如)

e,a,b,c,d

e,d,a,b,c

e,d,c,b,a

我希望它显示

e,a,b,c,d

a,e,b,c,d

a,b,e,c,d

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    这里有一个更高效简洁的解决方案,依赖于原生实现的System.arraycopy

    public static void moveLastup(String[] arr, int pos) {
        String last = arr[arr.length-1];
    
        // Copy sub-array starting at pos to pos+1
        System.arraycopy(arr, pos, arr, pos + 1, arr.length - pos - 1);
    
        arr[pos] = last;
    }
    

    还有一些测试代码:

    public static void main(String[] args) {
        String[] test = { "one", "two", "three", "four", "five" };
    
        // Move "five" to index 2
        moveLastup(test, 2);
    
        // [one, two, five, three, four]
        System.out.println(Arrays.toString(test));
    }
    

    关于您的编辑:您正在使用和修改原始数组。如果您想在每个moveLastup 中“重新开始”,您需要处理一个副本。这个 sn-p 打印你想要的:

    String[] list = { "a", "b", "c", "d", "e" };
    
    for (int pos = 0; pos < list.length; pos++) {
        String[] tmpCopy = list.clone();
        moveLastup(tmpCopy, pos);
        showList(tmpCopy);
    }
    

    输出:

    [e, a, b, c, d]
    [a,e, b, c, d]
    [a, b,e , c, d]
    [a, b, c,e, d]
    [a, b, c, d,e]
    p>

    【讨论】:

    • 你是认真的吗?那是我 10 分钟前写的,你也做了 x>=position,这是一种浪费。
    • 有什么办法可以改变原来的方法而不是第二个 for 语句,让它显示“重新开始”?
    • 嗯,不是真的。在执行下一个之前,您需要“撤消”上一个 lastMoveup
    【解决方案2】:

    我知道问题是关于 数组 - 不想开始讨论性能“与”问题以及“我为什么使用纯数组” - 但对于那些使用 List 我认为这可能有用。

    java.util.Collections.rotate 方法。是的,这个名字很奇怪,通过查看 javadoc 的一部分:

    请注意,此方法可以有效地应用于子列表 移动列表中的一个或多个元素,同时保留 其余元素的顺序。例如下面的成语 将索引 j 处的元素向前移动到位置 k(必须大于等于j):

    Collections.rotate(list.subList(j, k+1), -1);

    为了具体化,假设列表包含 [a,b,c,d,e]。移动索引 1 处的元素 (b) 向前两个位置,执行以下调用:

    Collections.rotate(l.subList(1, 4), -1);

    结果列表是 [a, c, d, b, e]。

    要向前移动多个元素,请增加绝对值 的旋转距离。要向后移动元素,请使用正数 移动距离。

    public void moveElement(List list, int a, int b) {
        // forward or backward
        int direction = a > b ? 1 : -1;
        // always from minor to major to subList
        int minor = a < b ? a : b;
        int major = b > a ? b : a;
        Collections.rotate(list.subList(minor, major + 1), direction);
    }
    

    【讨论】:

      【解决方案3】:

      首先,在你的代码中

      for (int x = stuff.length-1; x > pos; x--)  
      

      甚至没有定义 pos 的地方,我建议将其更改为位置。 其次,将“list”改为“stuff”。

      修改后的工作代码:

      public static void moveLastup(String[] stuff, int position) 
          {
              String y = stuff[stuff.length-1];
      
              for (int x = stuff.length-1; x > position; x--)  
                  stuff[x] = stuff[x-1];
      
              stuff[position] = y;
          }
      

      【讨论】:

        【解决方案4】:

        您没有使用List&lt;String&gt; 而不是String[] 的任何特殊原因?它将使这些类型的操作更容易。有了ArrayList&lt;String&gt;,您只需要:

        list.add(3, list.remove(list.size() - 1));
        

        如果你使用LinkedList&lt;String&gt;,甚至更短:

        list.add(3, list.removeLast());
        

        这是一个基于您的更完整的示例:

        LinkedList<String> list = new LinkedList<String>();
        list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
        list.add(3, list.removeLast());
        System.out.println(list); // prints "[a, b, c, e, d]"
        

        【讨论】:

          【解决方案5】:

          基于System.arraycopy的另一个简短快速的解决方案:

          System.arraycopy(array, insert, array, insert+1, array.length-insert-1);
          

          数组内容从索引“插入”开始“向右推”。

          下面是一些演示代码:

          int[] array = {1,2,3,4,5};
          int insert = 2;
          int last = array[array.length-1];
          System.arraycopy(array, insert, array, insert+1, array.length-insert-1);
          array[insert] = last;
          for (int value:array) System.out.println(value);
          

          【讨论】:

            【解决方案6】:

            你可以这样做:

            char arr1[]=new arr1[5]; // arr1 contains a,b,c,d,e in order
            char arr2[]=new arr2[5];
            
            for(int i=0;i<arr1.length;i++) {       // copy contents to arr2
               arr2[i]=arr1[i];
            }
            
            for(int i=0,j=4;i<arr1.length;i++) {
              arr2[i]=arr1[4];
              arr2[i+1]=arr1[4];
              arr2[i+2]=arr1[i+1];
              arr2[i+3]=arr1[i+2];
              arr2[i+4]=arr1[i+3];
            }
            
            for(int i=0;arr2.length;i++) {          // Final result
              System.out.println(arr[i]+" ");
            }
            

            【讨论】:

              猜你喜欢
              • 2014-12-06
              • 2016-06-21
              • 1970-01-01
              • 2021-11-24
              • 1970-01-01
              • 2020-06-30
              • 2014-08-07
              • 2022-01-22
              • 1970-01-01
              相关资源
              最近更新 更多