【问题标题】:Removing combinations from a permutation从排列中删除组合
【发布时间】:2016-11-07 22:22:16
【问题描述】:

我对 Java 和编程还很陌生,并且一直在尝试找到一种方法来完成我现在想做的事情。我想从这个字符串数组排列中删除一些组合,例如 (1,2,3,4,5,6) 或 (1,2,3,4,5,7)。我尝试过过滤或 .remove ,但似乎它们不适用于字符串数组。基本上我希望能够过滤掉不需要的组合的结果。这是我到目前为止所拥有的。

import java.util.Arrays;

public class Permutations {
  public static void main(String[] args) {

    String[] arraylist = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};

    combinations2(arraylist, 6, 0, new String[6]);
  }

  static void combinations2(String[] arraylist, int len, int startPosition, String[] result) {

    if (len == 0) {

      System.out.println(Arrays.toString(result));
      return;
    }
    for (int i = startPosition; i <= arraylist.length - len; i++) {
      result[result.length - len] = arraylist[i];
      combinations2(arraylist, len - 1, i + 1, result);
    }
  }
}

【问题讨论】:

  • "我已尝试过滤或 .remove" 请在您的代码中显示此内容。

标签: java arrays string permutation


【解决方案1】:

除非您特别需要使用数组,否则我建议您切换到标准集合。它们易于使用,您无需传递索引,因为您可以使用子列表。下面的代码使用标准的contains 方法来过滤掉不需要的组合。

public class Combos {
    private static final List<List<String>> FILTER = Arrays.asList(
        Arrays.asList("1", "2", "3", "4", "5", "6"),
        Arrays.asList("1", "2", "3", "4", "5", "7"));

    static void combos(List<String> input, List<String> result) {
        if (result.size() == 6) {
            if (!FILTER.contains(result))
                System.out.println(result);
        } else {
            for (int i = 0; i < input.size(); i++) {
                result.add(input.get(i));
                combos(input.subList(i + 1, input.size()), result);
                result.remove(input.get(i));
            }
        }
    }

    public static void main(String[] args) {
        combos(IntStream.rangeClosed(1, 13)
                .mapToObj(Integer::toString)
                .collect(Collectors.toList()), 
                new ArrayList<>());
    }
}

【讨论】:

  • 当我尝试使用您的代码时,它说无法解析数组,并且无法将列表解析为一种类型。我是否使用排列、集合或列表并不重要。我只想实现创建 6 个元素长度的组合并过滤掉 2、3、4、5、6、7 和 1、3、4、5、6、8 等特定组合的结果。
  • 您需要添加导入语句才能访问这些类。
【解决方案2】:

你在正确的轨道上。我注意到的一件事是,在您的main 方法中,您使用combinations2(arraylist, 6, 0, new String[6]); 行调用您的combinations2 方法。我想知道为什么您可能会将您的 result 存储在 new String[] 中,这会在您调用它后立即失去范围(本质上您没有将解析后的数组存储在这里 - 我建议将其存储到另一个为了有目的地使用数组,你可以看下面的代码 sn -p 例如。)。

根据回答问题,我会研究System.arrayCopy 方法,并像这样使用它:

public class App {
    public static void main(String[] args) {
        String[] arraylist = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
        System.out.println("arraylist: " + java.util.Arrays.toString(arraylist));
        String[] result= new String[arraylist.length - 6];
        System.arraycopy(arraylist, 6, result, 0, arraylist.length - 6);
        System.out.println("result: " + java.util.Arrays.toString(result));
    } 
}

输出;

arraylist: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
result: [7, 8, 9, 10, 11, 12, 13]

此外,从 1.5 及更高版本开始,有 Arrays.copyOfRange(Object[] src, int from, int to)

但是,如果您不按顺序删除,则进行多次 System.arraycopy() 调用将变得相当乏味/重复(*例如,在您的第二种情况下 (1,2,3,4,5,7 ) *)。

【讨论】:

  • 您建议的内容有效,但它删除了 6 元素格式。我想保留我拥有的格式,但删除我不想保留的特定组合。我不确定如何从您在这里向我展示的内容中实现这一目标。
  • 您能否解释更多,或者在您的原始帖子之前/之后的示例中进行编辑?与 System.arraycopy 相比,使用 Collections 可能会更好地处理特定组合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
相关资源
最近更新 更多