【问题标题】:Moving several items in an ArrayList在 ArrayList 中移动多个项目
【发布时间】:2014-12-05 13:53:23
【问题描述】:

我在 ArrayList 中有以下数据。为方便起见,假设它是一个 String ArrayList。

Mokey
MokeyBaby1
MokeyBaby2
MokeyBaby3
Dog
DogBaby1
DogBaby2
Cat
CatBaby1

我需要将相关的项目一起移动。

例如: Moving Monkey down。新的 ArrayList 如下所示。

Dog
DogBaby1
DogBaby2
Mokey
MokeyBaby1
MokeyBaby2
MokeyBaby3
Cat
CatBaby1

我已经有一个方法可以告诉我哪些 ArrayList 索引是相关的。

例如:getRelatedIndexes("Monkey") 将为原始列表返回 0,1,2,3

我只需要知道是否有一种简单的方法可以将 ArrayList 中的所有项目一起向上或向下移动。

谢谢。

【问题讨论】:

  • 如果我理解您的意思,您希望为每种动物获得一个ArrayList 吗? monkeyList = animalsList.magicMethod(getRelatedIndexes("Monkey")) 之类的东西?
  • 为什么没有列表列表?
  • 大家好,要明确一点,这不是字符串的 ArrayList,所以我无法对其进行排序。 @fxm
  • @mkrakhin 不,因为对象也需要单独(并且独立)存在。
  • @john 你的意思是他们并不总是被依次列出吗?

标签: java arraylist


【解决方案1】:

您可以将您的列表包装在一个可重新排序的列表中,并通过它实现您的重新排序 - 至少您不需要破解主列表。它将保持 int 数组中的顺序,然后您可以随意移动。如果您愿意,您甚至可以按几个不同的顺序维护相同的数据。

public static class OrderedList<T> extends AbstractList<T> {

    // The list I proxy.
    private final List<T> it;
    // The order.
    private final int[] order;

    public OrderedList(List<T> wrap) {
        it = wrap;
        order = new int[it.size()];
        // Initially the same order.
        for (int i = 0; i < order.length; i++) {
            order[i] = i;
        }
    }

    @Override
    public T get(int index) {
        return it.get(order[index]);
    }

    @Override
    public int size() {
        return it.size();
    }

    // TODO - Only moves up! Breaks on a down move.
    public void move(int start, int length, int to) {
        int[] move = new int[length];
        // Copy it out.
        System.arraycopy(order, start, move, 0, length);
        // Shift it down.
        System.arraycopy(order, start + length, order, start, to - start);
        // Pull it back in.
        System.arraycopy(move, 0, order, to, length);

    }
}

public void test() {
    List<String> t = Arrays.asList("Zero", "One", "Two", "Three", "Four", "Five");
    OrderedList<String> ordered = new OrderedList(t);
    System.out.println(ordered);
    ordered.move(1, 2, 3);
    System.out.println(ordered);
}

打印

[Zero, One, Two, Three, Four, Five]
[Zero, Three, Four, One, Two, Five]

或者 - 使用 Collections.rotate 并确定应该以哪种方式轮换子列表以实现您的移动。

【讨论】:

    【解决方案2】:

    也许这包含您需要的解决方案(交换和/或旋转/子列表)-Moving items around in an ArrayList

    【讨论】:

    • 并非如此。这个问题是关于仅上下移动 1 个项目。我想搬几个。
    • 更新:我正在研究轮换。我会尝试一下并更新这个问题。
    【解决方案3】:

    您可以搜索列表中的项目,这将满足您的条件并将它们安全地保存到另一个临时列表中。然后使用addAll(int index, Collection&lt;? extends E&gt; c) 方法将这些元素再次添加到列表中。那么您不必为每个元素本身使用add(int index, E element)

    【讨论】:

      【解决方案4】:

      块移位策略可以通过

      实现
      • 使用 List.remove(index) 将元素从原始列表中取出并添加到新的临时数组中。请注意,这必须以相反的顺序完成,否则索引会随着项目的删除而改变。
      • 使用 List.addAll(index, collection) 将新的临时数组添加到所需位置
      • 克隆的索引列表,以防它在其他地方使用

      例子

      public static void main(String[] args) {
          List<Animal> animals = new ArrayList<Animal>(Arrays.asList(new Animal(
                  "Mokey"), new Animal("MokeyBaby1"), new Animal("MokeyBaby2"),
                  new Animal("MokeyBaby3"), new Animal("Dog"), new Animal(
                          "DogBaby1"), new Animal("DogBaby2"), new Animal("Cat"),
                  new Animal("CatBaby1")));
      
          int[] relatedIndexes= { 0, 1, 2, 3 };
          shift(animals, relatedIndexes, 3);
      
          System.out.println(animals);
      }
      
      private static void shift(List<Animal> original, int[] indexes, int newIndex) {
          int[] sorted = indexes.clone();
          Arrays.sort(sorted);
          List<Animal> block = new ArrayList<Animal>();
          for (int i = sorted.length - 1; i >= 0; i--) {
              block.add(original.get(sorted[i]));
              original.remove(i);
          }
          original.addAll(newIndex, block);
      }
      

      输出

      [Dog, DogBaby1, DogBaby2, Mokey, MokeyBaby1, MokeyBaby2, MokeyBaby3, Cat, CatBaby1]
      

      【讨论】:

      • 感谢您抽出宝贵的时间。我的意思是,在示例中,我比较了字符串并按字母顺序排序。这不是实际情况。实际上我想在不同的条件下排序,它不是字母顺序(或根本与字符串相关),(这部分由getRelatedIndexes方法处理。)。我希望我已经说清楚了。
      猜你喜欢
      • 2016-06-14
      • 2022-12-10
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多