【问题标题】:I want to generate all combinations and permutations of an int array in java, but at a specified starting point我想在java中生成一个int数组的所有组合和排列,但是在一个指定的起点
【发布时间】:2015-02-04 07:31:28
【问题描述】:

我已经看遍了,很多人都想知道如何生成 int 数组的所有排列,例如int[] arr = {0, 1, 2}

但我想知道如何做一些更困难的事情。

我想在给定长度和最大数量的情况下生成int[]。 例如:

length = 4
max = 2

所以这意味着该方法必须生成一个长度为 4 的 int[],并且数组中的每个元素也必须是 0-2 之间的数字。

另外,第一次调用该方法时,必须返回[0,0,0,0] 第二次,必须返回[1,0,0,0] 以此类推,直到到达[2,2,2,2]

最后,我希望能够将“起点”作为参数传递给它。所以如果我给它0作为起点,它会返回[0,0,0,0],但是如果我给它2作为起点,它会返回[2,0,0,0]

我正在制作一个 100x100 像素的图像。我正在为 int[] 中的每个像素设置值

我想做的每个生成每个像素的每个组合,生成一堆随机像素,但我想以系统的方式来做,而不是仅仅给每个像素一个随机值。

【问题讨论】:

  • 您的问题是什么?你试过什么?
  • 原生类型是不可能的。只需创建自己的类并实现行为。
  • 当然可以。只需要几个循环。
  • 如果没有任何分量(R、G、B 或 A)的值大于 2,那么您生成的每张图像在人眼看来都是全黑的。
  • 我知道。我使用较低的值来简化问题。如果概念证明适用于低值,它将适用于更高的值。

标签: java arrays combinations permutation


【解决方案1】:

本质上,您希望生成具有指定值的 4 位基数为 3 的整数;并且您想将该整数表示为int[],并以最低有效位在前。 (例如,2 是基数为 3 的 0002,因此您需要 [2,0,0,0]。)

其实很简单:

private final int mLength;
private final int mMax;

private int[] generateArray(int value) {
    final int[] result = new int[mLength];
    for (int i = 0; i < mLength; ++i) {
        result[i] = value % (mMax + 1);
        value /= (mMax + 1);
    }
    return result;
}

(我假设您可以管理编写跟踪当前值的方法的部分,并使用generateArray 为该值生成一个数组,依此类推。)

【讨论】:

  • 哇,你只需要很少的代码就搞定了。我试着回答这个问题,但我的代码太乱了
  • 您好,感谢您的回答。但是我实际上想要做的是生成一个 300x300 图像,我只想通过 int[] 为图像中的每个像素设置值我稍微更改了你的代码以使其适用于我的程序但我保留即使我更改了值,也会得到完全相同的结果,以下是我所做的更改: public static int[] generateArray(int arrayLength, int maxVal, int value){ final int[] result = new int[arrayLength]; for (int i = 0; i
  • @s3t:对不起,我不明白你的评论。 “通过 int[] 设置图像中每个像素的值”是什么意思?这与你原来的问题有什么关系?在您的情况下,“价值”是什么?
  • @ruakh。对不起。我可能把这个问题说得太笼统了。我已经对其进行了编辑以提供更多信息。
【解决方案2】:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GenerateAllPosibleArrayWithLengthAndMaxValue {

    protected final int min;
    protected final int max;
    protected final int size;

    protected transient List<int[]> result;

    protected GenerateAllPosibleArrayWithLengthAndMaxValue(Builder builder) {
        this.min = builder.min;
        this.max = builder.max;
        this.size = builder.size;
    }

    public List<int[]> generate() {
        prepare();
        generate(newArray(), 0, min);
        return result;
    }

    protected void prepare() {
        this.result = new ArrayList<>();
    }

    protected void generate(int[] array, int index, int value) {
        if(index >= size)
            return;
        for(int v = min ; v <= max ; v++) {
            array[index] = v;
            generate(array, index + 1 , 0);
            if(index == size - 1)
                result.add(newArray(array));
        }
    }

    protected int[] newArray() {
        return newArray(min);
    }

    protected int[] newArray(int value) {
        int[] array = new int[size];
        Arrays.fill(array, value);
        return array;
    }

    protected int[] newArray(int[] array) {
        return Arrays.copyOf(array, array.length);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        protected int min = 0;
        protected int max = 3;
        protected int size = 5;

        public Builder min(int min) {
            this.min = min;
            return this;
        }
        public Builder max(int max) {
            this.max = max;
            return this;
        }
        public Builder size(int size) {
            this.size = size;
            return this;
        }

        public GenerateAllPosibleArrayWithLengthAndMaxValue build() {
            return new GenerateAllPosibleArrayWithLengthAndMaxValue(this);
        }
    }

    public static void main(String[] args) {
        GenerateAllPosibleArrayWithLengthAndMaxValue object = 
                GenerateAllPosibleArrayWithLengthAndMaxValue.builder()
                .min(0)
                .max(1)
                .size(3)
                .build();
        List<int[]> list = object.generate();
        System.err.println("result.size = " + list.size() + "\n");
        System.err.println(Strings.toString(list));
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多